jQuery appending an array of elements

后端 未结 9 705
星月不相逢
星月不相逢 2020-12-13 23:34

For the purpose of this question lets say we need to append() 1000 objects to the body element.

You could go about it like this:

         


        
9条回答
  •  天涯浪人
    2020-12-13 23:53

    Sometimes, jQuery isn't the best solution. If you have a lot of elements to append to the DOM, documentFragment is a viable solution:

    var fragment = document.createDocumentFragment();
    for(var i = 0; i < 1000; i++) {
        fragment.appendChild(document.createElement('div'));
    }
    document.getElementsByTagName('body')[0].appendChild(fragment);
    

提交回复
热议问题