其他分享
首页 > 其他分享> > 删除DOM节点

删除DOM节点

作者:互联网

删除节点

删除节点的步骤:先获取父节点,再通过父节点删除自己

<div id="father">
    <h1>标题一</h1>
    <p id="p1">p1</p>
    <p class="p2">p2</p>
</div>

<script>
    var self = document.getElementById('p1');
    var father = p1.parentElement;
    father.removechild(self)

    // 删除是一个动态的过程:
    father.removeChild(father.children[0])
    father.removeChild(father.children[1])
    father.removeChild(father.children[2])
</script>

注意:删除多个节点的时候,children是在时刻变化的,删除节点的时候一定要注意!

标签:p1,删除,DOM,father,节点,removeChild,children
来源: https://www.cnblogs.com/wshjyyysys/p/15894194.html