admin管理员组文章数量:1327750
thead = new Array();
alert(thead.length);
thead = document.getElementsByTagName("th");
alert(thead.length);
thead.pop();
alert(thead.length);
document.getElementsByTagName("th")
returns an array of elements, so thead
variable should be an array, if so, then why it gives me the error "thead.pop() is not a function"?
thead = new Array();
alert(thead.length);
thead = document.getElementsByTagName("th");
alert(thead.length);
thead.pop();
alert(thead.length);
document.getElementsByTagName("th")
returns an array of elements, so thead
variable should be an array, if so, then why it gives me the error "thead.pop() is not a function"?
-
Are you trying to remove from the DOM, or just from the list? If you actually want it removed from the DOM, do this...
var last = thead[thead.length - 1]; last.parentNode.removeChild(last);
The element will be removed from the DOM, as well as thethead
list. – user1106925 Commented Jul 5, 2012 at 16:24
3 Answers
Reset to default 7getElementsByTagName
(docs) does not return an Array
, it returns a NodeList
. As said by the linked NodeList
docs:
NodeList are used very much like arrays and it would be tempting to use Array.prototype methods on them. This is, however, impossible.
There are some Array
like things you can do with a NodeList
, and you can even .apply
some Array.prototype
methods to them, but you should read the docs to avoid the "gotchas", especially where problems with the NodeList
being "live" could bite you.
getElementsByTagName()
returns a DOM 2 NodeList
, not an Array
.
Technically, document.getElementsByTagName
returns a NodeList
object, which has no pop
function.
Try alert(Array.isArray(thead))
, and you'll see that it returns false
本文标签: Removing elements from an array pop is not a function (javascript)Stack Overflow
版权声明:本文标题:Removing elements from an array: pop is not a function (javascript) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742226494a2436414.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论