求教!!!为什么forEach里面不能输出document.write内容
扔个三星炸死你
扔个三星炸死你 2016-12-21 17:20:15

求教!!!为什么forEach里面不能输出document.write内容

var li = document.getElementsByTagName('li');

 li.forEach(function(x){document.write(x.nodeName+"  "+x.nodeValue+"  "+x.nodeType);});


扔个三星炸死你
扔个三星炸死你

reply all(1)
巴扎黑

因为forEach是用来遍历数组的,而通过documen.getElementByTagName得到的不是数组,是一个类似于数组的东西。所以不行。

var li = document.getElementsByTagName('li');

     var arr=[];

     for(var i=0;i

        arr.push(li[i]);

     }

     arr.forEach(function(x){document.write(x.nodeName+"  "+x.nodeValue+"  "+x.nodeType);});

这样子可以输出,不过反而麻烦了。


Popular topics
More>
Popular Tutorials
More>
Latest downloads
More>
web effects
Website source code
Website materials
Front end template
About us Disclaimer Sitemap
PHP Chinese website:Public welfare online PHP training,Help PHP learners grow quickly!