作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
每次标记其内容时,我都试图将输入元素聚焦在 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/
美好的一天!对不起我的英语。 有几个不同公司的 sample (标准具)邮票。并且有测试图像 - 可以出现这些邮票的文件照片。需要判断图片中是否有证件或那个印章(引用)。邮票可以旋转。 用通常的标准具
我是一名优秀的程序员,十分优秀!