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

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

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

Code :


  
    

        
2条回答
  •  被撕碎了的回忆
    2021-01-12 21:05

    the node is not actually removed

    The confusion might be because you might think removing an element means something like killing or destroying it.

    But in fact, the concept of removal basically means breaking the relationship between a child and its parent. It's just a detachment.

    Shouldn't I get an error while trying to insert an element which does not exist?

    No, because it still exists.

    How is remove different from removeChild?

    remove only needs a reference to the child. removeChild needs a reference both to the parent and the child. The result is identical.

    Is there any way to ensure that element is actually removed from memory?

    No. You can only unreference it and hope that there is a garbage collector which will detect the object is not referenced and then will remove it.

提交回复
热议问题