Copy Code code as follows:
<title></title>
<script type= "Text/javascript" >
function CreateNode () {
var pnode = document.createelement (' P ');
var tnode = document.createTextNode (' Fireworks March under Yang State ');
Pnode.appendchild (tnode);
Document.body.appendChild (Pnode);
}
function R () {
var pnode = document.createelement (' P ');
var tnode = document.createTextNode (' The Yellow Crane Tower of the West ");
Pnode.appendchild (tnode);
Get the node to replace
var renode = document.getelementsbytagname (' P ') [0];
This method only applies to IE browser
Renode.replacenode (Pnode, Renode);
Suitable for various browsers
ReNode.parentNode.replaceChild (Pnode, Renode);
}
function Renode () {
var oldNode = document.getelementsbytagname (' P ') [0];
Oldnode.parentnode returns the parent node of the P node, this is the body
Then use the RemoveChild method of the body node to delete the OldNode node below
OldNode.parentNode.removeChild (OldNode);
}
</script>
<body>
<input id= "Button1" type= "button" value= "Create Node" onclick= "CreateNode ()"/>
<input id= "Button2" type= "button" value= "Replace node" onclick= "R ()"/>
<input id= "Button3" type= "button" value= "Delete node" onclick= "Renode ()"/>
</body>