Add the node function insertBefore () to the specified node in js.

Source: Internet
Author: User

Add the node function insertBefore () to the specified node in js.

This article mainly introduces how to add the insertBefore () function to js at a specified position. The example analyzes the techniques of insertBefore () function appending nodes, which has some reference value, for more information, see

 

 

This article describes how to add the node function insertBefore () in js at a specified position. Share it with you for your reference. The specific analysis is as follows:

The function prototype is as follows:

InsertBefore (parameter 1, parameter 2): Add a node at a specified position

The Code is as follows:

The Code is as follows:

<Html>
<Head>
<Script type = "text/javascript">
Function t (){
Var nodeli = document. createElement ('lil'); // create a li Node
Var li_text = document. createTextNode ('blue sky'); // create a text node
Nodeli. appendChild (li_text); // append the text node to the li Node

Var nodeul = document. getElementsByTagName ('ul ') [0]; // obtain the first ul Node
Var nodeli1 = nodeul. getElementsByTagName ('lil') [2]; // obtain the 3rd nodes under ul-Autumn
Nodeul. insertBefore (nodeli, nodeli1); // The insertBefore () function indicates which node to add. The first parameter is the new node to be inserted, and the second parameter is the existing node.
}
</Script>
</Head>
<Body>
<Div id = "container">
<Ul>
<Li> spring </li>
<Li> summer </li>
<Li> autumn </li>
<Li> Winter </li>
</Ul>
</Div>
<Hr/>
<Button onclick = "t ()" value = ""> Add a node at a specified position </button>
</Body>
</Html>

 

I hope this article will help you design javascript programs.

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.