Javascript-The Createdocumentfragment () method of document

Source: Internet
Author: User
Tags tagname

In the 6.3.5: Creating and Manipulating nodes section of the JavaScript Advanced programming program, there are several ways to dynamically create HTML nodes, including the following common methods:

· Crateattribute (name): Create an attribute node with the name specified

· Createcomment (text): Create a comment node with text

· Createdocumentfragment (): Create a document fragmentation node

· CreateElement (tagname): Create a node with a label named TagName

· createTextNode (text): Creates a text node that contains textual text

One of the most interesting and previously untouched methods is the Createcomment (text) method, The book says: When you update a small number of nodes can be added directly to the Document.body node, but when you want to add a lot of data to the document is, if you add these new nodes directly, this process is very slow, because each add a node will call the parent node AppendChild () method, in order to solve this problem, you can create a document fragment, attach all the new nodes to it, and then add the document fragments to the documents at once.

If you want to create 10 paragraphs, you might write this code in a regular way:

123456 for(vari = 0 ; i < 10; i ++) {    varp = document.createElement("p");    varoTxt = document.createTextNode("段落"+ i);    p.appendChild(oTxt);    document.body.appendChild(p);}

Of course, this code runs without a problem, but he calls 10 times document.body.appendChild () to produce a page render every time. Fragments are very useful at this time:

1 varoFragment = document.createDocumentFragment();
12345 for(var i = 0 ; i < 10; i ++) {    varp = document.createElement("p");    varoTxt = document.createTextNode("段落"+ i);    p.appendChild(oTxt);    oFragment.appendChild(p);<br>}
1 document.body.appendChild(oFragment);

In this code, each new <p/> element is added to the document fragment, and the fragment is passed as a parameter to AppendChild (). The call to AppendChild () does not actually append the fragment of the document to the BODY element, but only the child nodes in the fragment, and then you can see a noticeable performance increase, document.body.appenChild () replaced 10 times, This means that only one content rendering refresh is required.

Javascript-The Createdocumentfragment () method of document

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.