thead = new Array();
alert(thead.length);
thead = document.getElementsByTagName("th");
alert(thead.length);
thead.pop();
alert(thead.length);
document.getElementsByTagName("th")
返回一个元素数组,所以 thead
变量应该是一个数组,如果是这样,那为什么它给我错误“thead.pop() 不是函数”?
最佳答案
getElementsByTagName
( docs ) 不返回一个 Array
,它返回一个 NodeList
.正如链接的 NodeList
文档所说:
NodeList are used very much like arrays and it would be tempting to use Array.prototype methods on them. This is, however, impossible.
有一些 Array
类的东西你可以用 NodeList
做,你甚至可以 .apply
一些 Array.prototype
方法,但您应该阅读文档以避免“陷阱”,尤其是在 NodeList
处于“实时”状态的问题可能会困扰您的地方。
关于javascript - 从数组 : pop is not a function (javascript) 中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11348429/