NodeList 对象是一个节点的集合,是由 Node.childNodes和 document.querySelectorAll返回的,实则是一个类数组对象。
Although
NodeListis not anArray, it is possible to iterate over it withforEach(). It can also be converted to a realArrayusingArray.from().
However, some older browsers have not implementedNodeList.forEach()norArray.from(). This can be circumvented by usingArray.prototype.forEach()— see this document's Example.
在主流浏览器和高版本IE下,可以直接使用forEach进行遍历,但对于IE11以下的版本,将无法执行遍历,下面提供一种方法对低版本IE做兼容
NodeList.prototype.forEach = Array.prototype.forEach;
直接在NodeList对象原型上指定forEach为Array的forEach,从而实现了NodeList的forEach遍历。
本文探讨了NodeList对象在不同浏览器中的实现差异,并提供了一种兼容旧版IE的方法,使其能够使用Array的方法如forEach进行迭代。
1万+

被折叠的 条评论
为什么被折叠?



