怎样遍历NodeList对象

因为NodeList对象是一个类似数组的对象, 且它自带了一个 forEach() 方法, 因此可以使用 forEach() 遍历, 它的用法和 Array 里面的 forEach() 是完全一样的.

document.querySelectorAll('li').forEach((item,i,obj)=>{console.log(i + " - " + item.textContent)})

这里的item为当前元素, i为索引, obj为整个NodeList.

原文地址:https://www.cnblogs.com/aisowe/p/11511877.html