gpt4 book ai didi

javascript - polymer 模板有邮票事件吗?

转载 作者:太空狗 更新时间:2023-10-29 16:40:47 26 4
gpt4 key购买 nike

每次标记其内容时,我都试图将输入元素聚焦在 polymer 模板中。问题是在加载模板之前我无法选择输入元素。目前,我只是在模板加载后 100 毫秒使用 setTimeout 来关注输入,但我想知道是否有更优雅的解决方案。此外,自动对焦属性不起作用,因为模板可能会多次取消标记和重新标记。现在,我的代码看起来像这样(这是在 polymer 元素定义中):

Polymer({

// ...

showInput: false,

makeInputVisible: function() {
this.showInput = true;
var container = this.$.container;
setTimeout(function() {
container.querySelector("#input").focus();
}, 100);
},
});
<div id="container">
<template if="{{showInput}}">
<input id="input" is="core-input" committedValue="{{inputValue}}" />
</template>
</div>

但我更喜欢这样的东西:

Polymer({

// ...

showInput: false,

makeInputVisible: function() {
this.showInput = true;
},

focusInput: function() {
this.$.container.querySelector("#input").focus();
},
});
<div id="container">
<template if="{{showInput}}"
on-stamp="{{focusInput}}">
<input id="input" is="core-input" committedValue="{{inputValue}}" />
</template>
</div>

欢迎任何想法。

最佳答案

在 Polymer 1.0 中,模板在标记时会触发一个“dom-change”事件。但是,使用 dom-if 模板会产生显着的性能成本,因为它们需要操作 dom 树。做这样的事情会好得多:

<div id="container">
<input id="input" hidden="[[!showInput]]" value="{{inputValue::input}}">
</div>
observers: [
'_showInputChanged(showInput)',
],

_showInputChanged: function (showInput) {
if (showInput) {
this.$.input.focus();
}
},

关于javascript - polymer 模板有邮票事件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28953336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com