Replace node (ReplaceChild ())

Source: Internet
Author: User

ReplaceChild (): Method replaces one child node within a given parent element with another child node:

Referencre = Element.replacechild (newchild,oldchild);

The Oldchild node must be an element child node whose return value is a reference pointer to the child node that has been replaced.

In the following example, the element with the id attribute value is content and an element with an id attribute value is fineprint, we will create a new text element with the createelement () method, and then use the ReplaceChild () method to replace the FinePrint element with the newly created element:

var container = document.getElementById ("content"); var message = document.getElementById ("FinePrint"); var para = document.createelement ("P"); Container.replacechild (para,message);

If the inserted element itself has child nodes, those child nodes are also inserted before the target node.

The ReplaceChild () method can also replace another existing node with an existing node on the document tree. If the newchild () node is an existing node on the document tree, the ReplaceChild () method will first delete the word and use it to replace the Oldchild () node.

In the following example, the ReplaceChild () method uses the id attribute value to be the headline element to replace the element that contains the id attribute value of the content element: FinePrint.

var container = document.getElement.ById ("content"); var message = document.getElementById ("FinePrint"); var announcement = document.getElementById ("headline"); Container.replacechild (announcement,message);

Replace node (ReplaceChild ())

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.