jQuery appending an array of elements

后端 未结 9 709
星月不相逢
星月不相逢 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:09

    A slight change to your second approach:

    var elements = [],
    newDiv;
    for (x = 0; x < 1000; x++) {
        newDiv = $('
    ').text(x); elements.push(newDiv); } $('body').append(elements);

    $.append() certainly can append an array: http://api.jquery.com/append/

    .append(content) | content: One or more additional DOM elements, arrays of elements, HTML strings, or jQuery objects to insert at the end of each element in the set of matched elements.

提交回复
热议问题