Summary of differences between createElement and createDocumentFragment

Source: Internet
Author: User

Most of the information that can be found on the Internet is that createDocumentFragment is used mainly because it avoids the efficiency problem caused by multiple times of adding createElement to document. body. For example: Copy codeThe Code is 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 the first example of my google, does not mean that you have any opinion on the original author, the original address http://www.cnitblog.com/asfman/articles/32614.html)
This statement is correct, but not rigorous, because it is mainly used when the target node already exists and has some content, such as the body element in the above example, if the target node does not exist or is empty, you can use createElement to create an identical element, and then use appendChild or replaceChild to achieve the same purpose, this does not cause repeated refresh.
Although I usually mix the two, I still have to understand the difference between them:
First:
The elements created by createElement can use innerHTML, while those created by createDocumentFragment can only be used as an attribute to modify the document content as expected. The node types of the two are completely different, and the elements created by createDocumentFragment do not have corresponding tags in the document. Therefore, they can only be accessed in js on the page.
Demo:Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Html>
<Head>
<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>
</Head>
<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 = 'strong paint power ';
Document. body. appendChild (fragment_2 );
</Script>
</Body>
</Html>

Second, another major difference is that the elements created by createElement can be operated repeatedly. After being added, even if they are removed from the document and still owned by the document, the operation can be continued, but the elements created by createDocumentFragment are one-time, after adding the clips, you cannot perform any operations. (Note: the newly created clips are not added here. As mentioned above, the newly created clips do not have corresponding tags in the document, all child nodes of the clip are added here ).
A comparison example:Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Html>
<Head>
<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>
</Head>
<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); // an error is reported because fragment does not exist in the document.
SetTimeout (function (){
Outer. removeChild (fragment );
}, 1000)
}, 1000)
}
</Script>
</Body>
</Html>

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.