Javascript 聚合物1.0-按id选择动态元件的问题
我正在尝试按id选择动态创建的元素:Javascript 聚合物1.0-按id选择动态元件的问题,javascript,web-component,polymer-1.0,Javascript,Web Component,Polymer 1.0,我正在尝试按id选择动态创建的元素: <dom-module id="filter-box"> <style> :host { display: block; } </style> <template> <h4 id="title">{{title}}</h4> <paper-checkbox id="test">test</paper-checkb
<dom-module id="filter-box">
<style>
:host {
display: block;
}
</style>
<template>
<h4 id="title">{{title}}</h4>
<paper-checkbox id="test">test</paper-checkbox><br>
<template is="dom-repeat" items="{{filters}}">
<paper-checkbox id="{{item}}">{{item}}</paper-checkbox><br>
</template>
</template>
</dom-module>
<script>
(function () {
Polymer({
is: 'filter-box',
properties: {
filters: {
type: Array,
notify: true,
},
title: {
type: String
}
},
ready: function() {
this.filters = [
'Commercial',
'Enterprise'
];
},
isSelected: function(filter) {
return this.$$[filter].checked;
}
});
})();
</script>
我可以通过此$.title选择标题,但是我不能按照建议以这种方式或使用此$$选择动态元素。根据您提供的参考,您应该调用
this。$$(选择器)
使用括号作为函数调用(而不是使用括号)。因此,用以下代码替换您的代码:
return this.$$('#'+filter).checked;
请注意,您可能还需要在id选择器前面加上
#
,感谢您的响应,但此。$$(…)仍返回未定义。您是否尝试过此。$$('#'+筛选器)。选中代码>?Id选择器以#
开头。谢谢!我认为“#”是隐式的。它不是隐式的,因为它是一个CSS样式选择器,因此可以是标记名、类名等。
return this.$$('#'+filter).checked;