开发者

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

开发者 https://www.devze.com 2023-02-22 13:49 出处:网络
If I delete a parent element, do the child elements remain in the browser\'s memory? <div id=\"parent\">

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);

</scr开发者_运维百科ipt>


No, they don't, don't worry. :)


If the children stays in the memory that'd be a very major flaw and a huge memory leak in the browsers code.


I hope you mean parentNode there :). parent is for accessing a parent frameset or window I believe.

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.


The child nodes are removed and taken care of they don't stay back in memory..

Alternative to your code you can use...jQuery

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

精彩评论

暂无评论...
验证码 换一张
取 消