Knowledge of javascript File Fragment _ javascript skills

Source: Internet
Author: User
Today, I encountered a problem: I used ajax to retrieve the Html of another page. However, I don't want to output these HTML files directly. I want to use getElementsByName to process them before outputting the corresponding content. How can I deal with it with native javascript? Later, a technical group asked about the following code:

The Code is as follows:


Function html2node (s ){
Var d = document. createElement ('P ');
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.

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.