javascript - 从数组 : pop is not a function (javascript) 中删除元素

标签 javascript arrays function

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/

相关文章:

javascript - 您如何缩小内部和数组的 promise 功能?

java - 如何将Map值添加到ArrayList中?

c# - 返回多种类型

javascript - Angular 10-拖动时不触发(单击)功能

javascript - 提交前检查数据

javascript - Webpack url-loader 内联 jpg 文件

java - 如何确保数组中没有任何内容被询问两次?

python - 在 python 中复制列表

Java 函数中的泛型

javascript - 使用 javascript 和 dom 从单选按钮创建输入