简体   繁体   English

从DOM中删除一个元素(及其子元素)

[英]Remove an element ( and its children ) from the DOM

Via the MDN reference I want to simply do: 通过MDN参考,我想简单地做一下:

parent_element.removeChild(child_element);

However, in this particular case child_element has its own child elements. 但是,在这种特殊情况下, child_element具有自己的子元素。

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. 是的,当您从DOM中删除一个元素时,其所有子元素都将随之删除。 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中的位置或方式可能存在。

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

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