迭代DOM集合的几种方法

1.  Array.prototype.slice.call()    转数组再遍历

var a= document.querySelectorAll('div');
var arr = Array.prototype.slice.call(a);
console.log(arr);

 2.Array.from() 将类数组转为数组

var a= document.querySelectorAll('div');
var arr = Array.from(a);
arr.forEach(function(i){
  console.log(i);
})

 3.for of

let elements = document.querySelectorAll('div');

for (let element of elements) {
  console.log(element.tagName);
}

 

posted @ 2018-11-28 09:38  安筱雨  阅读(283)  评论(0编辑  收藏  举报