删除节点
删除节点的步骤: 先获取父节点,再通过父节点删除自己
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<div id="father"><h1>标题1</h1><h1>标题2</h1><p id="p1">p1</p><p class="p2">p2</p>
</div><script>// 方式一let p1 = document.getElementById('p1');let father = p1.parentElement;// father.removeChild(p1)// 方式二, 方式二是一个动态的过程,删除一个少一个,所以 [] 里面的数字不能从 0~n-1console.log(father.children)all_ch = father.children// father.removeChild(father.children[0])father.removeChild(all_ch[0])father.removeChild(all_ch[1])father.removeChild(all_ch[1])father.removeChild(all_ch[0])
</script></body>
</html>
注意: 删除多个节点的时候,children 是在时刻变化的,删除节点的时候一定要注意
https://www.bilibili.com/video/BV1JJ41177di?p=22