removeChild真的会删除元素吗?
removeChild
函数真的能彻底删除子节点吗?或者它只是删除指定父节点的子元素?如果没有真正删除元素,有没有办法彻底删除元素?
Does removeChild
function really delete the child node completely? Or it just removes the element being child of the specified parant node? If it doesn't really deletes the element, is there a way to delete the element completely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
removeChild
方法只是将其从其父级中删除。如果它是页面的可见元素,则会从页面中删除。但Javascript有垃圾收集功能。这意味着只要有任何变量引用它,节点对象本身就会一直存在。因此,您可以将节点分配给变量,使用
removeChild
将其从其父节点中“删除”,然后将其插入或附加到其他节点,从而有效地在页面上移动它。以下代码将删除一个节点,并等待 10 秒,然后将其重新添加到树中(从而添加到页面中):
这意味着节点对象尚未从内存中删除,因为仍然有一个变量指向它(即
oldNode
)。另一种情况:
另一方面,如果您不将删除的节点重新分配给另一个变量,则无法再访问它(不能通过文档树访问,因为它已从那里删除;而不是通过 JS 变量);所以Javascript会自动从内存中清除它:
将删除的节点分配给一个变量,然后将
null
(或其他任何东西)分配给该变量 - 就像Marc B在他的回答中建议的那样 - 完全没有必要,恕我直言,愚蠢。The
removeChild
method simply removes it from its parent. If it’s a visible element of the page, it will be removed from the page.But Javascript has garbage collection. This means that the node object itself will remain in existence as long as any variable refers to it. So you can assign a node to a variable, use
removeChild
to 'prune' it from its parent node, and later on, insert or append it to some other node, thereby effectively moving it around on the page.The following code will remove a node, and wait 10 seconds before re-adding it to the tree (and thus, to the page):
This means that the node object hasn’t been deleted from memory, because there’s still a variable pointing to it (namely,
oldNode
).Another case:
If, on the other hand, you don’t reassign the removed node to another variable, it can’t be accessed anymore (not via the document tree, since it’s been removed from there; and not via a JS variable); so Javascript will automatically purge it from memory:
Assigning the removed node to a variable, and then assigning
null
(or anything else) to that variable — like Marc B suggests in his answer — is completely unnecessary and, IMHO, silly.这将完全删除节点:
这将从 DOM 中删除节点,使其不可见,但会保存它,以便您可以将其插入其他地方:
This will completely delete the node:
This will remove the node from the DOM so it's not visible but will save it so that you can insert it elsewhere:
removeChild 会从 dom 中删除该元素,但它也会从函数中返回,以防您执行删除操作以将其重新插入到其他位置。您必须杀死该返回值才能真正摆脱已删除的节点:
removeChild removes the element from the dom, but it's also returned from the function in case you're doing the removal to re-insert it elsewhere. You'd have to kill that return value to really get rid of the removed node:
如果你想真正删除一个dom元素。仅靠removeChild 是不够的。这是 YSlow 的作者 Steve Sounders 所说的。您需要使用删除
If you want to really delete a dom element . removeChild alone is not enough. This is as per Steve Sounders who is the author of YSlow. You need to use delete
快速提示:使用现代 JavaScript,您可以使用remove() 方法简化删除 DOM 元素的过程。只需使用node.remove(),而不是node.parentElement.removeChild(node)。这是达到相同结果的更干净、更简洁的方法。
Quick Tip: With modern JavaScript, you can simplify removing DOM elements using the remove() method. Instead of node.parentElement.removeChild(node), simply use node.remove(). It's a cleaner and more concise way to achieve the same result.