什么是NodeList对象
NodeList对象是dom节点的集合。document.querySelectorAll(selector)
和Node.childNodes
返回的都是NodeList对象,看着很像数组,却没有数组的方法。
属性
NodeList对象只有一个属性:length,它返回NodeList对象长度。
方法
NodeList对象只有一个方法:item ( index ),返回在index位置的dom节点对象。
遍历
使用数组的forEach方法对NodeList进行遍历,其它的数组方法同理使用
// 获取所有.list-item元素
var list = document.querySelectorAll('.list-item');
//为每个.list-item元素添加touchstart事件
[].forEach.call(list, function(element) {
console.log(element)
});