Javascript 按标签名称删除元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14003606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove element by tag name
提问by Mr. 1.0
Possible Duplicate:
use remove() on multiple elements
可能的重复:
在多个元素上使用 remove()
I am trying to remove all elements that have a tag name of "label". I have the following code. It works to some degree, but it only removes 1 label. The other 5 still remain. How can I change the code to remove all "label" tags?
我正在尝试删除标签名称为“label”的所有元素。我有以下代码。它在某种程度上起作用,但它只删除了 1 个标签。剩下的5个还在。如何更改代码以删除所有“标签”标签?
element = document.getElementsByTagName("label");
for (index = 0; index < element.length; index++) {
element[index].parentNode.removeChild(element[index]);
}
回答by Tomalak
var element = document.getElementsByTagName("label"), index;
for (index = element.length - 1; index >= 0; index--) {
element[index].parentNode.removeChild(element[index]);
}
回答by seliopou
The problem is that document.getElementsByTagName()returns a NodeList, not an Array. The content, and therefore length, of a NodeListis updated when you remove an element from the DOM that's in the NodeList. So when you remove the first element, the NodeListgets shorter and a newfirst element occupies index0. Updating indexin the loop therefore makes you miss at least one element, possibly more depending on the length of the result.
问题是document.getElementsByTagName()返回 a NodeList,而不是 an Array。的内容,因此长度,NodeList当你从那是在DOM中删除元素被更新NodeList。因此,当您删除第一个元素时,第一个元素NodeList会变短,并且新的第一个元素占用index0。index因此,在循环中更新会使您至少错过一个元素,根据结果的长度可能更多。
Try something like this:
尝试这样的事情:
var elements = document.getElementsByTagName('label')
while (elements[0]) elements[0].parentNode.removeChild(elements[0])
回答by Ja?ck
When you remove the nodes from a document, the list of nodes that you just obtained from getElementsByTagName()gets updated too to avoid lingering references, so you should just keep removing the first node until none remain.
当您从文档中删除节点时,您刚刚从中getElementsByTagName()获取的节点列表也会更新以避免延迟引用,因此您应该继续删除第一个节点,直到没有节点为止。
var nodes = document.getElementsByTagName("label");
for (var i = 0, len = nodes.length; i != len; ++i) {
nodes[0].parentNode.removeChild(nodes[0]);
}
Here, the value of nodes.lengthis cached; otherwise it would keep decreasing and you end up only removing half :)
这里,nodes.length缓存了 的值;否则它会继续减少,你最终只会删除一半:)
回答by jAndy
The "problem" is that .getElementsByTagName()returns a Live NodeList. Hence, when you remove the head (first) entry, the tail fills up and movesto the left, so to speak.
“问题”是.getElementsByTagName()返回一个Live NodeList。因此,当您删除头部(第一个)条目时,尾部会填满并向左移动,可以这么说。
Its not a real Javascript Arraywhere this would work as expected. To create such a frozen Arraywe can go like
它不是一个真正的Javascript 数组,它可以按预期工作。要创建这样一个冻结数组,我们可以像
var element = Array.prototype.slice.call(document.getElementsByTagName("label"),0);
for (var index = 0, len = element.length; index < len; index++) {
element[index].parentNode.removeChild(element[index]);
}
回答by Rob
Why not use jQuery? Then it's simply
为什么不使用jQuery?那么就简单了
$("label").remove();

