The appenchild,insertbefore--createdocumentfragment in JS

Source: Internet
Author: User

Usually there will be some process in the project, or comment related things, these will only be in the first part of the page load, the rest of the more tags, when the click More time, Ajax request all the data loaded (of course, there are pagination implementation, this article is not discussed), AJAX-loaded data will generally be applied to the Appendchild,insertbefore method to append to the original label, I believe that the loop attach must be very skilled, here I would like to write another way to compare the performance of the method-the use of document fragments , In the low version of IE, the lack of optimization mechanism, cyclic additional caused by the return and rendering, will keep the browser hold (geometric changes will cause the browser to rearrange)

Look at the general appendchild practice, of course, you can directly innerhtml or jquery HTML to the container, this is to consider the original HTML may contain events before the HTML string into a node.

1 varEle=document.createlement ("div");2 3ele.innerhtml=html;//Ajax gets to the4 5 varDivs=ele.childnodes;//So child nodes6 7  for(vari=0,length=divs.length;i<length;i++)8 9   {Ten  OneContainer.appendchild (Divs[i].clonenode (true));//cloning to a container that needs to be attached A  -}
View Code

Optimization:

 1  //  2  var  fragment=document.createdocumentfragment ();  3  for  (var  i=0;length=divs.length;i++) {  5  Fragment.apendchild (Divs[i].clonenode ( true   6  }  8  //  9  container.appendchild (fragment); 
View Code

Extended prototypes:

1 //ie9+2 //IE678 hides the access to HtmlElement .3 //please use Var appendhtmlop=function (el,html) {4 //El.appendchild (frament);//You cannot use This,this to point to window;5 //}6htmlelement.prototype.appendhtmlop=function(HTML) {7            varDiv=document.createelement ("div"), nodes=NULL,8Fragment=document.createdocumentfragment ();9 TenDiv.innerhtml=html; Onenodes=Div.childnodes; A                for(vari=0,length=nodes.length;i++){ -Fragment.appendchild (Nodes[i].clonenode (true)); -             } the            This. appendchild (fragment); -           //Release -nodes=NULL; -Fragment=NULL; +  -}
View Code

The same can be directly modified InsertBefore, the last appendchild the previous change to InsertBefore;

Of course, you can pass an identification parameter to distinguish;

1 varappendhtmlop=function(el,html,where) {2     varDiv=document.createelement ("div"), fragment=document.createdocumentfragment (); nodes=NULL;3 4where=where| | " Bottom;5Div.innerthtml=html;6nodes=Div.childnodes;7   for(vari=0,length=nodes.length;i<length;i++){8Fragement.appendchild (Nodes[i].clonenode (true));9    }Tenwhere!== "before"?El.appendchild (fragment): El.insertbefore (fragment,el.firstchild); One}
View Code

This is the document fragment optimization method, to reduce the number of appendchild directly appended to the HTML, so that its rendering geometry value increases;

The appenchild,insertbefore--createdocumentfragment in JS

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.