"JS Learning notes" DOM action App-Create, insert, and delete elements; document fragmentation

Source: Internet
Author: User

I. Creating, inserting, and deleting elements

(1) Creating DOM elements

CreateElement (sign) Create a node

AppendChild (node) append one node

Example: inserting LI for UL

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<script>
Window.onload = function ()
{
var Obtn=document.getelementbyid (' btn1 ');
var Oul=document.getelementbyid (' Ul1 ');
var Otxt=document.getelementbyid (' Txt1 ');

Obtn.onclick = function ()
{
var oli=document.createelement (' Li ');/* Create a good Li, the system does not know where to put it, we need to set the * *
Oli.innerhtml=otxt.value;
Parent,. appendchild (sub-node);
Oul.appendchild (oLi);/*oli added to oul*/as a child node
};
};
</script>

<body>
<input id= "Txt1" ytpe= "text"/>
<input id= "btn1" type= "button" value= "Create Li"/>
<ul id= "UL1" >
</ul>
</body>

(2) Inserting elements

InsertBefore (node, original node) in existing element money insertion

Example: Reverse Insert Li

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<script>
Window.onload = function ()
{
var Obtn=document.getelementbyid (' btn1 ');
var Oul=document.getelementbyid (' Ul1 ');
var Otxt=document.getelementbyid (' Txt1 ');

Obtn.onclick = function ()
{
var oli=document.createelement (' Li ');/* Create a good Li, the system does not know where to put it, we need to set the * *
var ali=oul.getelementsbytagname (' Li ');
Oli.innerhtml=otxt.value;
Parent,. appendchild (sub-node);
Oul.appendchild (oLi);/*oli added to oul*/as a child node
This code is designed to be compatible with IE
if (ali.length>0)
{
Oul.insertbefore (oli,ali[0]) */* insert before NO. 0 Li */
}
Else
{
Oul.appendchild (oLi); * * Append a node directly using appendchild if there is no previous Li */
}

};
};
</script>

<body>
<input id= "Txt1" ytpe= "text"/>
<input id= "btn1" type= "button" value= "Create Li"/>
<ul id= "UL1" >
</ul>
</body>

(3) Removing DOM elements

RemoveChild (node) Delete a node

Example: Remove Li

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Delete Elements </title>
<script>
Window.onload = function ()
{
var aa=document.getelementsbytagname (' a ');
var Oul=document.getelementbyid (' Ul1 ');

for (Var i=0;i<aa.length;i++)
{
Aa[i].onclick = function ()
{
Oul.removechild (This.parentnode);
};
}
};
</script>
<body>
<ul id= "UL1" >
<li>aaaaa<a href= "javascript:;" > Delete </a></li>
<li>bbbbbb<a href= "javascript:;" > Delete </a></li>
<li>cccccc<a href= "javascript:;" > Delete </a></li>
<li>dddddd<a href= "javascript:;" > Delete </a></li>
</ul>
</body>

Second, document fragmentation

(1) Document fragmentation can improve the performance of the DOM (theoretically), but only in the low-level browser, if in the advanced browser, this method may lead to performance is not improved but decreased.

For example, to insert 10,000 Li tags, if there is no document fragmentation, you must insert one and then render the page once, insert the full part will be rendered 10,000 times, too lossy performance. With document fragmentation, you can first put all the changes you need into the document fragment and render again.

(2) Document fragmentation principle

(3) Document.createdocumentfragment ()

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Document Fragmentation 2</title>
<script>
Window.onload = function ()
{
var Oul=document.getelementbyid (' Ul1 ');
var ofrag=document.createdocumentfragment ();/* Create document Fragment */

for (Var i=0;i<10000;i++)
{
var oli=document.createelement (' Li ');

Ofrag.appendchild (oLi) */* directly into the document fragment * *

}
Oul.appendchild (Ofrag);/* Load the document fragment one at a time into the Oul */
};
</script>

<body>
<ul id= "UL1" >

</ul>
</body>

"JS Learning notes" DOM action App-Create, insert, and delete elements; document fragmentation

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.