AppendChild () Issues to be aware of when inserting nodes

Source: Internet
Author: User

1th: First appendchild The value returned when inserting the node is the inserted node itself, and the parent node that you want to insert must also be found:

1 var returnnode = parentnode.appendchild (childnode); 2 Console.log (Returnnote===childnode); // true

2nd: AppendChild The new node to be inserted if it exists in the flow of the document, then the heart node is not copied and then inserted under the parent node, but instead, the new node is moved from the source document stream to the parent node to be inserted:

1 <DivID= "Test">2     <Div>Adscasdjk</Div>3     <DivID= "a">Adscasdjk</Div>4 </Div>5 <Scripttype= "Text/javascript">6     varT=document.getElementById ("Test");7     vara=document.getElementById ('a');8     //var tt = A.clonenode (true);9 T.appendchild (a);Ten </Script>

This has the JS code and no JS code performance is the same, but in the firebug can be clearly found that the ID of the div moved to the no ID of the div before, that is, id= ' a ' div just moved to the parent node.

1 <DivID= "Test">2     <Div>Adscasdjk</Div>3     <DivID= "a">Adscasdjk</Div>4 </Div>5 <Scripttype= "Text/javascript">6     varT=document.getElementById ("Test");7     vara=document.getElementById ('a');8     varTT=A.clonenode (true);9 t.appendchild (TT);Ten </Script>

The solution is to clone the node you want to insert into the appropriate location so that you can achieve the desired effect.

AppendChild () Issues to be aware of when inserting nodes

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.