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.