How to add a list-item at specific position

前端 未结 4 1748
被撕碎了的回忆
被撕碎了的回忆 2020-12-24 10:51

I\'d like to add a

  • at a specific position, for example:

    • Position 1
    • Posi
  • 相关标签:
    4条回答
    • 2020-12-24 11:32

      Check insertAfter here

      0 讨论(0)
    • 2020-12-24 11:39
      $('<option val="position3">Position3</option>').insertAfter('#list :nth-child(<give childnumber here>)')
      

      index start with 1

      0 讨论(0)
    • 2020-12-24 11:44

      You have to use after() instead of append():

      Description: Insert content, specified by the parameter, after each element in the set of matched elements.

      $('#list li:eq(1)').after('<li>Position 3</li>');
      

      The documentation of append() clearly says:

      Insert content (...) to the end of each element.


      For completeness:

      Note that :eq(n) matches the nth element of the matching element set, whereas :nth-child(n) matches the nth child of the parent.

      0 讨论(0)
    • 2020-12-24 11:52

      You should use after() or insertAfter()
      http://api.jquery.com/category/manipulation/dom-insertion-outside/

      0 讨论(0)
    提交回复
    热议问题