1. 程式人生 > >叠代DOM集合的幾種方法

叠代DOM集合的幾種方法

proto wrap title ice message doc slice 集合 select

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);
}

叠代DOM集合的幾種方法