简体   繁体   English

如何在不去parentNode的情况下删除父元素?

[英]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.

相关问题 当我只有1个父级时,使用parentNode.removeChild使用Javascript删除父级元素? - Remove parent element with Javascript using parentNode.removeChild when I only have 1 parent? 如何获取和移除querySelectorAll的parentNode - How to get and remove the parentNode of querySelectorAll Jquery - 如果子元素没有文本,如何删除父元素? - Jquery - How to remove parent element if child element is without text? 如何在不删除子级的情况下删除包装器(父元素)? - How can I remove wrapper (parent element) without removing the child? element.remove() 或 element.parentNode.removeChild(element) 更快吗? - Is element.remove() or element.parentNode.removeChild(element) faster? 如何删除父元素而不删除其内容? - How to remove parent element without deleting its contents? 如何在不超出父级的情况下让 div 内的元素跟随 cursor? - How do I get an element inside a div to follow the cursor without going outside the parent? JS:如何根据需要自动执行“ parentNode”上移次数 - JS: How to automate going up “parentNode” as many times as needed 如何访问选择的 parentNode 以提升元素? - How to access the parentNode of a selection to raise an element? 如何在 javascript 中获取 select 元素的父节点 - How to get parentnode of select element in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM