删除节点
删除节点的步骤,先获取父节点,在通过父节点删除自己
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h1>p1</h1>
<p id="p1">p2</p>
<p class="p2">p3</p>
</div>
<script>
//找到要删除的节点
var self=document.getElementById('p1');
//获取父节点
var father=self.parentElement;
//根据父节点 删除子节点
father.removeChild(self);
//删除节点是一个动态的过程
father.removeChild(father.children[0]);
father.removeChild(father.children[1]);
father.removeChild(father.children[2]);
</script>
</body>
</html>
注意:
删除节点的时候,children 是在时刻变化的,删除节点的时候一定要注意!