01 循环 NodeList

var doms = documents.querySelectorAll('span');

1. for 循环

兼容性最好,支持所有浏览器

for(var i = 0; i < doms.length; i++){
    var element = doms[i];
}

2. Array 的 forEach 函数

IE 9 及以上浏览器 Can I usearrow-up-right

Array.prototype.forEach.call(doms,function(element){
  // Do something with DOM
})
[].foreach.call(doms,function(element){
  // Do something with DOM
})

3. 继承 Array 的 forEach 函数

IE 9 及以上浏览器

4. 原生 Nodelist forEach 函数

2016 年以后版本的浏览器 Can I usearrow-up-right Global -> 89.72% MDN - NodeList.prototype.forEach()arrow-up-right 语法:NodeList.forEach(callback[, thisArg]);

Polyfill

或者

最后更新于