What is the difference between 'remove' and 'removeChild' method in javascript?

后端 未结 2 1250
名媛妹妹
名媛妹妹 2021-01-12 20:15

I have created an HTML page to understand how removing an element works.

Code :


  
    

        
2条回答
  •  孤独总比滥情好
    2021-01-12 20:42

    remove is a new function. It's a shortcut, making it simpler to remove an element without having to look for the parent node. It's unfortunately not available on old versions of Internet Explorer so, unless you don't want to support this browser, you'll have to use removeChild or use a polyfill.

    The reason the child element is kept in memory is your code keeps a reference to it.

    If you do

    childDiv = null;
    

    then there's no reference pointing to it and the browser can free the memory.

提交回复
热议问题