问题描述
this: document.getElementsByClassName('warningmessage')。remove();
如果页面上有多个warningmessage元素,则不起作用。
this: document.getElementsByClassName('warningmessage').remove();
doesn't work if you have multiple warningmessage elements on the page.
如何删除该类的所有元素?我必须为每个使用吗?是不是有一个命令deleteall()?
How can I just delete all elements with that class? do I have to use a for each
? isn't there a command to deleteall()?
感谢您的提示!
推荐答案
使用纯JavaScript,你可以这样做:
With plain JavaScript you can do this:
var nodes = document.getElementsByClassName('warningmessage');
for(var i = 0; i < nodes.length; i++){
nodes[i].parentNode.removeChild(nodes[i]);
}
所以你首先得到你感兴趣的节点然后迭代超过他们并将其从父母身上移除。
So you would first of all get the nodes you are interested in and afterwards iterate over them and remove them from their parents.
可悲的是,上没有
。但是,你可以这样: forEach
方法节点列表
Sadly there is no forEach
method on NodeList
. However, you could this:
var nodes = document.getElementsByClassName('warningmessage');
[].forEach.call(nodes, function (node) {
node.parentNode.removeChild(node);
});
这篇关于在多个元素上使用remove()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!