A brief summary of the point difference between createelement and createdocumentfragment _javascript skills

Source: Internet
Author: User
Most of the internet can be found that the use of createdocumentfragment mainly because of avoiding createelement multiple times added to the document.body caused by efficiency problems, such as:
Copy Code code as follows:

var arrtext=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
for (Var i=0;i<arrtext.length;i++) {
var op=document.createelement ("P");
var otext=document.createtextnode (Arrtext[i]);
Op.appendchild (Otext);
Document.body.appendChild (OP);
}
var arrtext=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
var ofrag=document.createdocumentfragment ();
for (Var i=0;i<arrtext.length;i++) {
var op=document.createelement ("P");
var otext=document.createtextnode (Arrtext[i]);
Op.appendchild (Otext);
Ofrag.appendchild (OP);
}
Document.body.appendChild (Ofrag);

(statement: This is my first example of Google, does not mean that the original author has any comments, the original address http://www.cnitblog.com/asfman/articles/32614.html)
This statement is not wrong, but not rigorous, because like this usage, mainly used in the target node is already existing and a part of the case, such as the above example of the BODY element, if the target node does not exist or is empty, you can use createelement to create an identical element, The operation is followed by a appendchild or replacechild to achieve the same purpose, so there is no problem of duplicate refreshes.
Although I usually mix the two, but still have to understand the difference between the two:
First:
Elements created by createelement can use the elements created by innerhtml,createdocumentfragment to use innerHTML and do not achieve the desired effect of modifying the contents of the document, just as a property. The node types of the two are completely different, and the elements created by createdocumentfragment do not have corresponding tags in the document, so they can only be accessed in JS on the page.
Demo
Copy Code code as follows:

<! 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></title>
<style type= "Text/css" >
#outer {height:200px; border:1px solid #006400;}
</style>
<body>
<div id= "outer" >
</div>
<input type= "button" value= "createelement" id= "btn_1"/><input type= "button" value= "Createdocumentfragment" Id= "Btn_2"/>
<script type= "Text/javascript" >
var fragment_1 = document.createdocumentfragment ();
fragment_1.innerhtml = ' <p> I am a painter </p> ';
Document.body.appendChild (fragment_1);
var fragment_2 = document.createelement (' P ');
fragment_2.innerhtml = ' painting ability strong ';
Document.body.appendChild (fragment_2);
</script>
</body>

Second: The other main difference is that the elements created by createelement can be repeated operations, added even if removed from the document is still owned by the document, you can continue to operate, but Createdocumentfragment created elements are one-time, After you add it, you won't be able to manipulate it (note: the addition here is not adding a newly created fragment because it says that the newly created fragment does not have a corresponding label inside the document, where all the child nodes of the fragment are added).
An example of contrast:
Copy Code code as follows:

<! 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></title>
<style type= "Text/css" >
#outer {height:200px; border:1px solid #006400;}
</style>
<body>
<div id= "outer" >
</div>
<input type= "button" value= "createelement" id= "btn_1"/><input type= "button" value= "Createdocumentfragment" Id= "Btn_2"/>
<script type= "Text/javascript" >
function $ (ID) {
return document.getElementById (ID);
}
var outer = $ (' outer ');
var inner = $ (' inner ');
$ (' btn_1 '). onclick = function () {
var div = document.createelement (' div ');
div.innerhtml = ' <p> test createelement</p> ';
Document.body.appendChild (DIV);
settimeout (function () {
Outer.appendchild (DIV);
settimeout (function () {
Outer.removechild (DIV);
},1000)
},1000)
}
$ (' btn_2 '). onclick = function () {
var p = document.createelement (' P ');
p.innerhtml = ' Test documentfragment ';
var fragment = Document.createdocumentfragment ();
Fragment.appendchild (P);
fragment.innerhtml = ' <p> test documentfragment</p> ';
fragment.innerhtml = ' <span> test documentfragment</span> ';
Document.body.appendChild (fragment);
settimeout (function () {
Outer.appendchild (fragment)//error, because the document can not exist inside the fragment
settimeout (function () {
Outer.removechild (fragment);
},1000)
},1000)
}
</script>
</body>

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.