<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function CreateNode() {
var pnode = document.createElement('p');
var tnode = document.createTextNode('烟花三月下扬州');
pnode.appendChild(tnode);
document.body.appendChild(pnode);
}
function ReplaceNode() {
var pnode = document.createElement('p');
var tnode = document.createTextNode('故人西辞黄鹤楼');
pnode.appendChild(tnode);
//获取要替换的p节点
var oldnode = document.getElementsByTagName('p')[0];
//oldnode.replaceNode(pnode,oldnode);//此种方法只支持IE
oldnode.parentNode.replaceChild(pnode, oldnode); //通用的
}
function RemoveNode() {
var oldnode = document.getElementsByTagName('p')[0];
//oldnode.parentNode返回的是p节点的父节点,这里就是body,然后使用body节点的removeChild方法删除其下面的pnode节点
oldnode.parentNode.removeChild(oldnode);
}
</script>
</head>
<body>
<input id="Button1" type="button" value="创建节点" onclick="CreateNode();" /><br />
<input id="Button2" type="button" value="替换节点" onclick="ReplaceNode();" /><br />
<input id="Button3" type="button" value="删除节点" onclick="RemoveNode();" />
</body>
</html>