简体   繁体   English

我是如何在Polymer 1.0元素中循环访问一组DOM元素的?

[英]How am I meant to loop through a set of DOM elements within a Polymer 1.0 Element?

I'm trying to loop through a set of static paper-checkbox elements, but my code is failing with: 我试图遍历一组静态的纸张复选框元素,但是我的代码失败:

"Uncaught TypeError: this.querySelectorAll(...).forEach is not a function" “未捕获的TypeError:this.querySelectorAll(...)。forEach不是函数”

The relevant line of code is: 相关的代码行是:

this.querySelectorAll('paper-checkbox').forEach(function(cb) {

I'm sure it's me being dumb - but what am I doing incorrectly in either selecting and/or iterating through the selected (static) checkboxes? 我确定这是我的愚蠢-但是在选择和/或遍历选中的(静态)复选框时,我在做什么错误?

I'm effectively looking for the Polymer 1.0 alternative to JQuery's .each() function. 我正在有效地寻找JQuery的.each()函数的Polymer 1.0替代方案。

Many thanks! 非常感谢!

Thanks for the responses. 感谢您的答复。 I've just found the solution here . 我刚刚在这里找到了解决方案。

Instead of: 代替:

this.querySelectorAll()

I should have been using: 我应该一直在使用:

Polymer.dom(this).querySelectorAll()

Works perfectly now! 现在工作完美!

Thanks again. 再次感谢。

The problem is this.querySelectorAll('paper-checkbox') returns a NodeList, not an Array. 问题是this.querySelectorAll('paper-checkbox')返回一个NodeList,而不是一个Array。 They looks similar but they are different. 它们看起来相似,但有所不同。 NodeList doesn't have the method foreach on its prototype. NodeList在其原型上没有foreach方法。

An easy solution is to convert your Nodelist to Array like this: Array.prototype.slice.call(document.querySelectorAll('paper-checkbox')) 一个简单的解决方案是将您的Nodelist转换为Array,如下所示: Array.prototype.slice.call(document.querySelectorAll('paper-checkbox'))

I suggest you to read this article in MDN on this topic. 我建议您阅读MDN中有关此主题的文章

This is because 这是因为

this.querySelector('paper-checkbox') 

is null. 一片空白。

I think you need to go into the shadow root to get the elements eg 我认为您需要进入影子根目录以获取元素,例如

 this.shadowRoot.querySelectorAll('paper-checkbox')

added: 添加:

this.shadowRoot.querySelectorAll('paper-checkbox').array().forEach(function(cb) {

Answer: You need to use dom-repeat . 答:您需要使用dom-repeat


According to the API documentation here : 根据此处API文档

"The dom-repeat element is a custom HTMLTemplateElement type extension that automatically stamps and binds one instance of template content to each object in a user-provided array." “ dom-repeat元素是一个自定义HTMLTemplateElement类型扩展,可以自动标记一个模板内容实例并将其绑定到用户提供的数组中的每个对象。”


Example Code: 示例代码:

 <dom-module id="employee-list"> <template> <div> Employee list: </div> <template is="dom-repeat" items="{{employees}}"> <div>First name: <span>{{item.first}}</span></div> <div>Last name: <span>{{item.last}}</span></div> </template> </template> <script> Polymer({ is: 'employee-list', ready: function() { this.employees = [{ first: 'Bob', last: 'Smith' }, { first: 'Sally', last: 'Johnson' }, ...]; } }); </script> </dom-module> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历一个数组和一组dom元素,并将数组的当前项添加到当前的dom元素? - How to loop through an array and set of dom elements and add the current item of the array to the current dom element? Polymer 1.0无法访问嵌套内的元素 <template> 元件 - Polymer 1.0 can't access elements within nested <template> element 如何遍历一组ID并匹配jQuery中另一组元素的活动元素属性? - How can I loop through a set of IDs and match the active element's attribute of another set of elements in jQuery? 如何遍历DOM中动态添加的元素 - How to loop through dynamically added elements in DOM 聚合物1.0:如何使该自定义元素具有声明性? - Polymer 1.0: how to make this custom element declarative? 如何在Polymer 1.0中调用元素的自定义方法? - How to call custom method of a element in Polymer 1.0? 如何在野生动物园的Shadow DOM中找到元素? (聚合物) - How can I find element in shadow DOM in safari? (Polymer) 是否可以从另一个元素访问元素函数?聚合物1.0 - is it possible to access an elements function from another element? polymer 1.0 如何使用dom-repeat在Polymer 1.0中创建动态菜单 - How to create a dynamic menu in Polymer 1.0 using dom-repeat 如果我在现有库中包含Polymer Platform,则DOM元素上的Set属性碰巧再次被取消定义 - Set properties on DOM elements happen to be undefined again if I include Polymer Platform on a existing library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM