JavaScript replaces existing elements ReplaceChild () use introduction _javascript tips

Source: Internet
Author: User
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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.