简体   繁体   中英

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.

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.

Thus, in JavaScript, how to I tell the GC "hey, get rid of this DOM element, it's no longer needed"?

Thanks!

To remove all elements, I suppose you could set element.innerHTML to an empty string (although I've never tried it myself). Otherwise, you could use element.removeChild(child) , as described here .

jQuery also supports $([selector]).remove([selector]) , which is more flexible in specifying which elements you want to remove at once. There's more information about jQuery remove here .

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