<html>
<body>
<h1>用DOM 增加一个 paragraph</h1>
<div id="demo">
<p id="p1">this is the paragraph to be removed</p>
<button onclick="myFunction()" id="button">remove</button>
</div>
<script>
var p = document.createElement("p");
var node = document.createTextNode("hello");
p.appendChild(node);
document.getElementById("demo").appendChild(p);
</script>
<h1>用DOM 移除一个 paragraph</h1>
<script>
function myFunction() {
document.getElementById("demo").removeChild(document.getElementById("p1"));
}
</script>
<div id="demo2"></div>
<h2>Summary:</h2>
<p>
1. 增加删除 主要使用的方法为 appendChild() & removeChild()<br>
2. 给 paragraph 增加一段文字,需要把文字当成一个node 使用 createTextNode<br>
3. 增加paragraph 使用 createElement<br>
</p>
<script>
document.write(document.getElementById("demo").getElementsByTagName("p").length);//看一共有几个p
//注意细节: getElement s
</script>
</body>
</html>
05-20
335
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)