[英]In JavaScript what is the right way to kill a DOM element?
I know that I can set it's style to "display: none" 我知道我可以将其样式设置为“显示:无”
However, that just hides it. 但是,这只是将其隐藏。
I really want to kill a DOM element and all of it's children. 我真的想杀死DOM元素及其所有子元素。
The context is that I'm building a desktop-like GUI system (for learning purposes) inside of a DOM, and when a "window" is closed, I want that DIV and all it's children to be removed. 上下文是,我正在DOM内构建类似桌面的GUI系统(出于学习目的),并且当“窗口”关闭时,我希望删除DIV及其所有子项。
Thus, in JavaScript, how to I tell the GC "hey, get rid of this DOM element, it's no longer needed"? 因此,在JavaScript中,如何告诉GC“嘿,摆脱这个DOM元素,它不再需要了”?
Thanks! 谢谢!
To remove all elements, I suppose you could set element.innerHTML
to an empty string (although I've never tried it myself). 要删除所有元素,我想您可以将
element.innerHTML
设置为空字符串(尽管我自己从未尝试过)。 Otherwise, you could use element.removeChild(child)
, as described here . 否则,你可以使用
element.removeChild(child)
,如所描述这里 。
jQuery also supports $([selector]).remove([selector])
, which is more flexible in specifying which elements you want to remove at once. jQuery还支持
$([selector]).remove([selector])
,它在指定要一次删除的元素上更加灵活。 There's more information about jQuery remove
here . 有一个关于jQuery的更多信息,
remove
这里 。
What about removeChild ? 那么removeChild呢?
See http://dustindiaz.com/add-and-remove-html-elements-dynamically-with-javascript/ for more information 有关更多信息,请参见http://dustindiaz.com/add-and-remove-html-elements-dynamically-with-javascript/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.