The example in this article describes the substitution or modification function replacechild () Usage of a DOM node. Share to everyone for your reference. The specific analysis is as follows:
The replacement process for DOM nodes:
(1) Create a new node;
(2) Find the old node;
(3) to stand at the parent node's point of view and replace it with the replacechild (new, old) function.
Copy Code code as follows:
<script type= "Text/javascript" >
function T () {
Train of thought: 1. First find the node to be replaced; 2. Create a new node; 3. Find the parent node and call the ReplaceChild (new, old) method at the parent node's point of view
var newli = document.createelement (' li ');//Create new node
var NewText = document.createTextNode (' daylight ');//Create Text node
Newli.appendchild (NewText);
var Nodeul = document.getelementsbytagname (' ul ') [0];//Find parent node
var Oldli = nodeul.children[2];//Find the node to replace
Nodeul.replacechild (Newli,oldli);//Replace
}
</script>
<body>
<div id= "Container" >
<ul>
<li> Spring </li>
<li> Summer </li>
<li> Blue Sky </li>
<li> Autumn </li>
<li> Winter </li>
</ul>
</div>
<div id= "Copyul" >
</div>
<button onclick= "t ()" value= "" > Node replacement and modification </button>
</body>
I hope this article will help you with your JavaScript programming.