It is very helpful for iterating the DOM of the bulk Operations page! Take advantage of document fragmentation, then append once and use native JavaScript statements to manipulate
Document.createdocumentfragment () is to save the use of DOM. Each JavaScript operation on the DOM will change the page's monetization and refresh the entire page, consuming a lot of time. To solve this problem, you can create a document fragment, attach all of the new nodes to it, and then add the contents of the document fragment to the file at once.
This is a simple test page I wrote:<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "
"
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>document.createdocumentfragment () test page </title>
<body>
<script type= "Text/javascript" >
varD1 =NewDate ();
for(vari=0;i<1000;i++)
{
varOp=document.createelement ("P");
varOtext=document.createtextnode ("Test1");
Op.appendchild (Otext);
Document.body.appendChild (OP);
}
varD2 =NewDate ();
document.write ("Method One time:" + (D2.gettime ()-d1.gettime ()) + "<br/>");
//---+-----
varD3 =NewDate ();
varofrag=document.createdocumentfragment ();
for(vari=0;i<1000;i++)
{
varOp=document.createelement ("P");
varOtext=document.createtextnode ("Test2");
Op.appendchild (Otext);
Ofrag.appendchild (OP);
}
Document.body.appendChild (Ofrag);
//in this piece of code
varD4 =NewDate ();
document.write ("Method two time:" + (D4.gettime ()-d3.gettime ()) + "<br/>");
</script>
</body>
Once the node is added to the document.body (or subsequent node), the page immediately reflects the change. For the first small program, running is no problem, but the problem is that it calls 10 times document.body.appendChild (), each time to produce a page refresh, which will produce a lot of page fragmentation. The second small piece of code, Document.body.appendChild (), is called only once, which means that only one page refresh is needed, and the time is obviously less than the previous one.
I used three kinds of browsers to test the above test code, the results are as follows:
IE7:
Method one time: 140
Method two time: 125
Firefox:
Method one time: 66
Method two time: 43
Chrome:
Method one time: 35
Method two time: 25
The results are consistent with the theory.