JS DOM Document Object Model node two [introductory article]

Source: Internet
Author: User
Tags object model

Copy nodes. CloneNode (Boolean): an argument:
Look at an example:
<body>
</body>
<script language= "JavaScript" >
var mes = document.createTextNode ("Hello World");
var container = document.createelement ("P");
Container.appendchild (MES);
Document.body.appendChild (container);
var Newpara = Container.clonenode (true); the difference between//true and false
Document.body.appendChild (Newpara);

var Newpara = Container.clonenode (false); the difference between//true and false
Document.body.appendChild (Newpara);
</SCRIPT>
Look at the results under Firebug:


See the difference between true and false.
true: <p>hello world</p> clone.
False: Clone <p></p> only, the text inside is not cloned.
As with createelement (), the cloned new node is not automatically inserted into the document. Need AppendChild ();
Another note: If the ID after cloning, do not forget to use setattribute ("id", "new_id");
To change the ID of the new node.

Inserts a node. AppendChild ():
The front is used several times, should probably use all know.
The specific explanation is:
Appends a child node to the element, and the new node is inserted at the end.
var container = document.createelement ("P");
var t = document.createtextnode ("Cssrain");
Container.appendchild (t);
Document.body.appendChild (container);

In addition, appendchild () can be used not only to append new elements, but also to reposition existing elements in the document.
Look at the following example:
<p id= "MSG" >msg</p>
<p id= "Content" >content</p>
<p id= "AAA" >aaaaaaaa</p>
<script>
var mes = document.getElementById ("msg");
var container = document.getElementById ("content");
Container.appendchild (MES);
</script>
MSG was found behind the content.
JS Internal processing mode:
First remove the ID msg from the document and insert it into the content as the last node of the content.
The results are:
<p id= "Content" >
Content
<p id= "MSG" >msg</p>
</p>
<p id= "AAA" >aaaaaaaa</p>

Inserts a node. InsertBefore ():
As the name implies, is to insert a new node in front of the target node.
Element.insertbefore (NewNode, Targernode);
Note that the first parameter is the new node, followed by the target node (the location of the insertion).
The new node is the guest, must first serve him ... ^_^

The second argument is optional, and if the second argument is not written, the default is added to the end of the document, equivalent to AppendChild ();
Let's see how the InsertBefore () is used:
<div id= "Cssrian" >
<p id= "Content" >1111</p>
<div id= "MSG" >msg<div>test</div></div>
<p id= "Content" >222</p>
<p id= "AAA" >aaaaaaaa</p>
</div>
<script>
var msg = document.getElementById ("msg");
var AAA = document.getElementById ("AAA");
var test = document.getElementById ("Cssrian");
Test.insertbefore (msg, AAA);
</script>
We found that the ID msg was inserted into the front of AAA.
JS Internal processing mode is similar to AppendChild (). Also:
Remove the ID msg from the document before inserting it into AAA before inserting it as a previous node of AAA.

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.