First understanding of javascript file fragments

Source: Internet
Author: User

Later, a technical group asked about the following code:
Copy codeThe Code is as follows:
Function html2node (s ){
Var d = document. createElement ('div ');
D. innerHTML = s;
If (d. childNodes. length = 1)
Return d. childNodes [0];
Var df = document. createDocumentFragment ();
While (d. firstChild)
Df. appendChild (d. firstChild );
Return df;
}

The general principle is clear. What is confusing is why document. createDocumentFragment is used?
After searching for related resources on the Internet, we found out that document. createDocumentFragment is used to create document fragments.
When we need a large number of appendChild page elements, we can first appendChild these elements into document. createDocumentFragment.
Then, you only need to fragment the appendChild document to the page. In this way, you do not need to refresh the page multiple times to achieve performance optimization. The code above I think it is unnecessary to use document fragments.

Related Article

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.