jQuery appending an array of elements

后端 未结 9 706
星月不相逢
星月不相逢 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-14 00:14

    Since $.fn.append takes a variable number of elements we can use apply to pass the array as arguments to it:

    el.append.apply(el, myArray);
    

    This works if you have an array of jQuery objects. According to the spec though you can append an array of elements if you have the DOM elements. If you have an array of html strings you can just .join('') them and append them all at once.

提交回复
热议问题