求教!!!为什么forEach里面不能输出document.write内容
var li = document.getElementsByTagName('li');
li.forEach(function(x){document.write(x.nodeName+" "+x.nodeValue+" "+x.nodeType);});
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
因为forEach是用来遍历数组的,而通过documen.getElementByTagName得到的不是数组,是一个类似于数组的东西。所以不行。
var li = document.getElementsByTagName('li');
var arr=[];
for(var i=0;i<li.length;i++){
arr.push(li[i]);
}
arr.forEach(function(x){document.write(x.nodeName+" "+x.nodeValue+" "+x.nodeType);});
这样子可以输出,不过反而麻烦了。