在不知道其父元素的情况下删除 dom 元素?
是否可以删除除 body 标签之外没有父级的 dom 元素?我知道使用像 jquery 这样的框架会很容易,但我试图坚持直接使用 javascript。
这是我发现的代码:
function removeElement(parentDiv, childDiv){
if (childDiv == parentDiv) {
alert("The parent div cannot be removed.");
}
else if (document.getElementById(childDiv)) {
var child = document.getElementById(childDiv);
var parent = document.getElementById(parentDiv);
parent.removeChild(child);
}
else {
alert("Child div has already been removed or does not exist.");
return false;
}
}
谢谢!
Is it possible to remove a dom element that has no parent other than the body tag? I know this would be easy with a framework like jquery, but I'm trying to stick to straight javascript.
Here's the code I've found to do it otherwise:
function removeElement(parentDiv, childDiv){
if (childDiv == parentDiv) {
alert("The parent div cannot be removed.");
}
else if (document.getElementById(childDiv)) {
var child = document.getElementById(childDiv);
var parent = document.getElementById(parentDiv);
parent.removeChild(child);
}
else {
alert("Child div has already been removed or does not exist.");
return false;
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您应该能够获取该元素的父级,然后从该
Update
中删除该元素。您可以将其设置为 HTMLElement 上的新方法:
然后执行
el.remove()< /code> (这也将返回元素)
You should be able to get the parent of the element, then remove the element from that
Update
You can set this as a new method on the HTMLElement:
And then do
el.remove()
(which will also return the element)适用于 Chrome 25.0.1364.155
请注意,这不适用于 IE11 或 Opera Mini,但所有其他浏览器都支持。
请参阅此处:caniuse 上的 childnode-remove 参考
works in Chrome 25.0.1364.155
Note that this does not work in IE11 or Opera Mini but is supported by all other browsers.
See here: reference to childnode-remove on caniuse
我认为你可以做类似的事情...
请参阅 node.parentNode 了解更多信息关于这一点。
I think you can do something like...
See node.parentNode for more info on that.
使用
outerHTML
属性删除元素Removing element using
outerHTML
property该函数使用 id 简单地删除一个元素:
This function to simply remove an element using id:
好吧,你基本上不需要知道父节点就可以从 DOM 中删除一个 DOM 元素,看下面的代码,看看 JavaScript 中删除节点元素的顺序是怎样的:
Element
+parentNode
+removeChild(Element);
如您所见,我们首先找到元素,然后使用 .parentNode,然后再次删除作为 Element 的子元素,因此我们不需要完全了解父母!
现在看看真正的代码:
或者作为一个函数
jQuery还有广泛使用的remove()函数,例如:
还有原生的
ChildNode.remove()
,它在IE和旧浏览器中不存在,但你可以对它进行 polyfill,请查看 MDN 中建议的 polyfill:如果您想了解更多信息,请访问此
OK, you basically don't need to know the parent to delete a DOM element from DOM, look at the below code, see how is the order to delete a node element in JavaScript:
Element
+parentNode
+removeChild(Element);
As you see we find the element first, then using .parentNode and then remove the child which is the Element again, so we don't need to know the parent at all!
So now look the real code:
or as a function
Also jQuery has remove() function which is widely use, like:
Also there is native
ChildNode.remove()
which is not in IE and old browsers, but you can polyfill it, look the suggested polyfill from MDN:If you like to learn more about it, visit this link on MDN.