简体   繁体   English

当我删除父本身时,是否需要删除DOM元素的所有子元素?

[英]Do I need to delete all the children of a DOM element , when I delete the parent itself?

If I delete a parent element, do the child elements remain in the browser's memory? 如果我删除父元素,那么子元素是否仍保留在浏览器的内存中?

<div id="parent">
    <ul>
        <li>one</li>
        <li>two</li>
        <li>one</li>
    </ul>
</div>
<script>

    var element = document.getElementById("parent"); 
    element.parent.removeChild(element);

</script>

No, they don't, don't worry. 不,他们不,不要担心。 :) :)

如果孩子们留在内存中,这将成为浏览器代码中的一个非常重大的缺陷和巨大的内存泄漏。

I hope you mean parentNode there :). 我希望你的意思是parentNode :)。 parent is for accessing a parent frameset or window I believe. parent是用于访问我相信的父框架集或window

It won't remain in memory if removed. 如果删除它将不会保留在内存中。 Otherwise a lot of memory would be leaking with objects in the DOM with no pointer to them. 否则,很多内存会在DOM中的对象泄漏而没有指向它们的指针。

The child nodes are removed and taken care of they don't stay back in memory.. 子节点被删除并处理它们不会留在内存中。

Alternative to your code you can use...jQuery 替代你的代码,你可以使用... jQuery

$('#parent').empty().remove();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要删除dom片段还是垃圾收集器会删除它们? - Do i need to delete dom fragments or will the garbage collector remove them? Javascript - 如何通过单击删除 DOM 元素(此命令) - Javascript - How do I delete the DOM Element by cliking (This command) 如何删除按钮上的DOM元素 - How do I delete DOM element on a button click 如果img src损坏,则删除父元素和所有子元素 - Delete parent element and all children if img src broken 在Polymer中,我如何使用 <template is=“dom-repeat”> 有值和删除自己的按钮? - in Polymer how do I make a list of elements using <template is=“dom-repeat”> with values and a button to delete itself? 如何在不向其父/子项添加逻辑的情况下获取组件中子元素的dom节点? - How do I get the dom node of a child element in a component without adding logic to its parent/children? 是否需要删除或删除创建但未附加到DOM的HTML元素? - Do I need to remove or delete HTML elements that I created but didn't attach to the DOM? 如何从 Selenium/Python 的父节点中删除/排除选定的子节点? - How do I delete/exclude selected children nodes from the parent node in Selenium/Python? 按删除按钮时如何删除父div - How do I get the parent div to delete when I press the delete button 从引导程序过滤时,如何从所有子项中删除显示样式? - How can I delete display style from all children when I filter from bootstrap?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM