js DOM 操作 appendChild()
下面的页面中有三个段落标签,我们在末尾再添加一个;
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>appendChild()</title>
</head>
<body>
<p id="first">我是第一个</p>
<p id="second">我是第二个</p>
<p id="third">我是第三个</p>
<script>
window.onload=function(){
var newp=document.createElement("p");//创建新的节点 p
newp.innerHTML="我是新加的"; //给新创建的节点p 添加内容
document.body.appendChild(newp); //在第二个节点之前插入新创建的节点
}
</script>
</body>
</html>
appendChild( ) 的用法:
document.body.appendChild( newp );
document.body 是要加的段落的父元素, appendChild ( newp )里面有一个参数,
这个参数就是我们要添加的新节点;