removeChild真的会删除元素吗?

发布于 2024-10-20 11:19:49 字数 85 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无远思近则忧 2024-10-27 11:19:49

removeChild 方法只是将其从其父级中删除。如果它是页面的可见元素,则会从页面中删除。

但Javascript有垃圾收集功能。这意味着只要有任何变量引用它,节点对象本身就会一直存在。因此,您可以将节点分配给变量,使用 removeChild 将其从其父节点中“删除”,然后将其插入或附加到其他节点,从而有效地在页面上移动它。

以下代码将删除一个节点,并等待 10 秒,然后将其重新添加到树中(从而添加到页面中):

var oldNode = someNode.removeChild(...);
setTimeout(function () {
  document.documentElement.appendChild(oldNode);
}, 10000);

这意味着节点对象尚未从内存中删除,因为仍然有一个变量指向它(即 oldNode)。

另一种情况:

var node = document.getElementById('test');
// ... do stuff
node.parentElement.removeChild(node);
// 'node' still exists, but has been removed from the page
// ... do some more stuff
node = document.getElementById('hello');
// The variable 'node' now points to something else; 
//  this means the original node will be deleted from memory

另一方面,如果您将删除的节点重新分配给另一个变量,则无法再访问它(不能通过文档树访问,因为它已从那里删除;而不是通过 JS 变量);所以Javascript会自动从内存中清除它:

someNode.removeChild(...);

将删除的节点分配给一个变量,然后将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):

var oldNode = someNode.removeChild(...);
setTimeout(function () {
  document.documentElement.appendChild(oldNode);
}, 10000);

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:

var node = document.getElementById('test');
// ... do stuff
node.parentElement.removeChild(node);
// 'node' still exists, but has been removed from the page
// ... do some more stuff
node = document.getElementById('hello');
// The variable 'node' now points to something else; 
//  this means the original node will be deleted from memory

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:

someNode.removeChild(...);

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.

红颜悴 2024-10-27 11:19:49

这将完全删除节点:

someNode.removeChild(...);

这将从 DOM 中删除节点,使其不可见,但会保存它,以便您可以将其插入其他地方:

oldNode = someNode.removeChild(...);

This will completely delete the node:

someNode.removeChild(...);

This will remove the node from the DOM so it's not visible but will save it so that you can insert it elsewhere:

oldNode = someNode.removeChild(...);
岛徒 2024-10-27 11:19:49

removeChild 会从 dom 中删除该元素,但它也会从函数中返回,以防您执行删除操作以将其重新插入到其他位置。您必须杀死该返回值才能真正摆脱已删除的节点:

oldNode = someNode.removeChild(...);
oldNode = null;

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:

oldNode = someNode.removeChild(...);
oldNode = null;
樱花落人离去 2024-10-27 11:19:49

如果你想真正删除一个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

戈亓 2024-10-27 11:19:49

快速提示:使用现代 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文