在我的变量上执行alert()会给我这个结果
[object NodeList]
我怎么才能看到其中所有的值?
注意:我在使用Firefox浏览器,不知道如何使用Chromebug,因此它没有安装。
在我的变量上执行alert()会给我这个结果
[object NodeList]
我怎么才能看到其中所有的值?
注意:我在使用Firefox浏览器,不知道如何使用Chromebug,因此它没有安装。
NodeList
中的值:for (var index = 0; index < nodeList.length; index++) {
alert(nodeList[index]);
}
这是一个好的资源,提供了更深入的信息:https://web.archive.org/web/20170119045716/http://reference.sitepoint.com/javascript/NodeList
如果您真的需要序列化,可以使用outerHTML。
// Firefox doesn't support outerHTML on nodes, so here's a method that does it
// https://dev59.com/VXI-5IYBdhLWcg3wu7Lv
function outerHTML(node){
return node.outerHTML || new XMLSerializer().serializeToString(node);
}
for (var index = 0; index < nodeList.length; index++) {
alert(outerHTML(nodeList[index]));
}
var elements = document.querySelectorAll('a');
for (var i = 0, element; (element = elements[i]); i++) {
console.log(element);
}
let elements = document.querySelectorAll('a');
for (let i = 0, element; (element = elements[i]); i++) {
console.log(element);
}
alert(Object.keys(nodeList[i]))
; - ShazNodeList
充满了Nodes
。你是否想要输出某些特定的属性?如果是这样,你可以使用alert(nodeList[i].attribute)
。或者你可以尝试使用alert(nodeList[i].innerHTML)
。 - arothalert(nodeList[i][0]);
怎么样? - Shaz