How do I assemble a
    using jQuery append()?

后端 未结 5 1394
谎友^
谎友^ 2020-12-29 20:17

I\'m trying to use the append function and encountered this:

$(\"#details\").append(\'
    \'); for (var i = 0; i < 10; i++) { $(\"#details\").ap
5条回答
  •  自闭症患者
    2020-12-29 21:04

    I think there's a problem in your loop

    for (var i = - i < 10; i++)
        $("#details").append('
  • something
  • ');

    should be I think

    for (var i = 0; i < 10; i++)
        $("#details ul").append('
  • something
  • ');

    and lose the following from the end

    $("#details").append('
');

Working Demo

EDIT:

Based on George IV's comment, in order to avoid appending

  • elements to any
      that may be a child of the element with id "details", simply give the
        element an id when you append it-

        $("#details").append('
          '); for (var i = 0; i < 10; i++) $("#appended").append('
        • something
        • ');

        you may also find this article interesting - 43,439 reasons to use append() correctly

  • 提交回复
    热议问题