The substitution or modification function of a DOM node ReplaceChild () Usage Instance _javascript skill

Source: Internet
Author: User

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.

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.