Detailed comparison between js AppendChild and insertBefore

Source: Internet
Author: User

Detailed comparison between js AppendChild and insertBefore

This article mainly compares AppendChild and insertBefore usage in js in detail. For more information, see.

We know that both appendChild and insertBefore have the function of inserting nodes. However, there are some differences between the two in terms of applications.

 

For example, when we want to insert a subnode P in the following div:

 

<Div id = "test"> <p id = "x1"> Node </p> <p> Node </p> </div>

 

We can write it like this (in some cases, add another comment ):

 

The Code is as follows:

<Script type = "text/javascript">

Var oTest = document. getElementById ("test ");

Var newNode = document. createElement ("p ");

NewNode. innerHTML = "This is a test ";

// The test starts from here.

// AppendChild method:

OTest. appendChild (newNode );

// InsertBefore method:

OTest. insertBefore (newNode, null );

</Script>

 

Through the above code, we can test that a new node is created under the node div, and this node is the last node of the div. (For DOM viewing, IE can be viewed through the IE Developer Toolbar plug-in, Firefox can use Firebug)

 

Obviously, through this example, we can know that both appendChildhild and insertBefore can be used to insert nodes.

 

In the above example, there is a code: oTest. insertBefore (newNode, null). Here, insertBefore has two parameters that can be set. The first parameter is the same as appendChild, but the second parameter is unique to it. It can be either null or:

The Code is as follows:

<Script type = "text/javascript">

Var oTest = document. getElementById ("test ");

Var refChild = document. getElementById ("x1 ");

Var newNode = document. createElement ("p ");

NewNode. innerHTML = "This is a test ";

OTest. insertBefore (newNode, refChild );

</Script>

 

In this example, a new node is inserted before the x1 node.

 

Or:

 

The Code is as follows:

<Script type = "text/javascript">

Var oTest = document. getElementById ("test ");

Var refChild = document. getElementById ("x1 ");

Var newNode = document. createElement ("p ");

NewNode. innerHTML = "This is a test ";

OTest. insertBefore (newNode, refChild. nextSibling );

</Script>

 

In this example, a new node is inserted before the next node of the x1 node.

 

It can also be:

The Code is as follows:

<Script type = "text/javascript">

Var oTest = document. getElementById ("test ");

Var newNode = document. createElement ("p ");

NewNode. innerHTML = "This is a test ";

OTest. insertBefore (newNode, oTest. childNodes [0]);

</Script>

 

In this example, a new node is inserted before the first subnode. You can also insert a new node in another location by changing childNodes [,...].

 

As you can see, the insertBefore () method inserts a new node before an existing subnode. However, in the example 1, The insertBefore () method can also be used to insert a new node at the end of the subnode list. The two conditions are combined to find that the insertBefore () method can insert nodes anywhere in the subnode list.

 

The following examples show that:

AppendChild () method adds a new subnode at the end of the subnode list of the node.

The insertBefore () method inserts a new node anywhere in the node's subnode list.

 

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.