ReplaceChild (A,B) is used to replace an existing element in a document.
Parameter A: the node to insert,
Parameter B: the node to replace
Copy Code code as follows:
var odiv = document.getElementById ("Guodiv");
var Ospan = document.createelement ("span");
ospan.innerhtml = "4";
var firschild = Odiv.firstelementchild? ODiv.firstElementChild:oDiv.firstChild
var returnnode = Odiv.replacechild (Ospan, firschild); Replaces the first element and returns the replaced element
alert (returnnode.innerhtml); 1
var lastchild = Odiv.lastelementchild? ODiv.lastElementChild:oDiv.lastChild;
Ospan = document.createelement ("span");
ospan.innerhtml = "5";
Returnnode = Odiv.replacechild (Ospan, LastChild); Replace the last one, return the replaced element
alert (returnnode.innerhtml);//3
Copy Code code as follows:
<div id= "Guodiv" >
<span>1</span>
<span>2 </span>
<span>3</span>
</div>