简体   繁体   English

如何完全删除父元素

[英]How to delete parent a element completely

if this is my div element 如果这是我的div元素

<a href="sdfsfsf"><p class="quotes">blablabla</p></a>

If it is not homepage, how can you delete parent a for this quotes 如果不是主页,如何删除此quotes父级a

i could not configure this code for my element: 我无法为我的元素配置此代码:

function delete_row(e)
{
    e.parentNode.parentNode.parentNode.removeChild(e.parentNode.parentNode);
}

js program must be automatically loaded also when page is loaded and it is not home page, it should delete a element js程序也必须在页面加载且不是主页时也自动加载,应该删除一个元素

要删除子元素e的直接父元素,而使子元素仍保留在DOM中(但重新绑定到其原始祖父母),请使用.replaceChild

e.parentNode.parentNode.replaceChild(e, e.parentNode);

If you want to leave p in place you need to add it to its parent's container first: 如果要将p保留在原位,则需要先将其添加到其父容器中:

function delete_row(e) {
    var parent = e.parentNode;
    var grandParent = parent.parentNode;
    // remove e from its parent
    e.remove();
    // insert e into its grandParent, before parent
    grandParent.insertBefore(e, parent);
    // remove parent
    parent.remove();
}

But the same can be accomplished by the solution of @Alnitak above. 但是,可以通过上述@Alnitak的解决方案来实现相同的目的。

Today I've learned :) 今天我学到了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM