简体   繁体   中英

How to remove the parent element without going to parentNode?

Im just wondering in order to remove a parent, you need to go to its parentNode. such as:

 div.parentNode.removeChild(div);

What happens if div has no parents? How can you remove div without going to its parents?

If a div element doesn't have parent, there is no place to remove this div from, so your div element doesn't exist in the page. It's safe to use div.parentNode.removeChild(div); because if the div element doesn't have a parent, it is already removed, or just never added to the page.

No need to go to the parent. Just use remove .

div.remove();

That will remove the exact node.

This is not supported in older browsers so you would need a polyfill for them as defined on this page: http://devdocs.io/dom/childnode/remove

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