nodeList在Mozilla中的定义是:
This is a commonly used type which is a collection of nodes returned by getElementsByTagName, getElementsByTagNameNS, and Node.childNodes. The list is live, so changes to it internally or externally will cause the items they reference to be updated as well. Unlike NamedNodeMap, NodeList maintains a particular order (document order). The nodes in a NodeList are indexed starting with zero, similarly to JavaScript arrays, but a NodeList is not an array.
一下这篇文章也讲述了怎样验证Array和NodeList的区别,并给出了NodeList转换为Array的方法
var anchors = document.getElementsByTagName("a");
var arr = Array.prototype.slice.call(anchors); //非ie浏览器正常
http://www.cnblogs.com/jeffwongishandsome/archive/2010/07/07/1773144.html
但是在for in遍历NodeList时会出现一个奇怪的问题
var lis = document.getElementsByTagName("li");
alert(lis.length);//13
for(i in lis){
alert(i);//0,1,2,3,4,5,6,7,8,9,10,11,12,length(chrome下)//length(ie8)
}
为什么会出现length?
W3help上的解释是这样的:http://w3help.org/zh-cn/causes/SJ5003
我的结论是:for in是把nodeList当做对象来遍历。
for in虽然方便,但是在不明确便利类型的时候乱用还是会带来不少麻烦的。