<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>
版权声明:本文为weixin_45852947原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。