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.