[英]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. 我只是想知道要删除父对象,您需要转到其parentNode。 such as:
如:
div.parentNode.removeChild(div);
What happens if div has no parents? 如果div没有父母怎么办? How can you remove div without going to its parents?
如何在不经过div父母的情况下删除div?
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. 如果div元素没有父元素,则无处可删除该div,因此您的div元素在页面中不存在。 It's safe to use
div.parentNode.removeChild(div);
使用
div.parentNode.removeChild(div);
是安全的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. 因为如果div元素没有父元素,则该元素已被删除,或者从未添加到页面中。
No need to go to the parent. 不用去找父母 Just use
remove
. 只需使用
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 较旧的浏览器不支持此功能,因此您需要按照此页面上的定义为其进行polyfill: http : //devdocs.io/dom/childnode/remove
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.