简体   繁体   中英

Remove an element ( and its children ) from the DOM

Via the MDN reference I want to simply do:

parent_element.removeChild(child_element);

However, in this particular case child_element has its own child elements.

Can I assume that this will not cause any problems and that they too will be removed.

The examples given in the reference did not make complete sense.

Yes when you remove an element from the DOM, all of its children are removed with it. If you are working with a modern browser, this is pretty safe. Older browsers tended to get memory leaks if you did not first remove all your event handlers before removing the elements.

Yes, all elements that are childs of a removed element are removed. You don't need to implement a deep removal yourself.

是的,您可以假设孩子的孩子将被遣散

如果这些子项没有被删除,则除非有人将它们放置在其他地方,否则它们在DOM中的位置或方式可能存在。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM