add li to middle of the ul (jquery)

前端 未结 3 637
难免孤独
难免孤独 2021-01-02 17:00

How one can add li to middle of the ul.

For example if i have list like this:

  • 1
  • 2
  • 3<
相关标签:
3条回答
  • 2021-01-02 17:39
    var middle = Math.ceil($("#myUL li").length / 2);
    $("#myUL li:nth-child(" + middle + ")").after('<li>Item New</li>');
    
    0 讨论(0)
  • 2021-01-02 17:41

    Try this:

    $('ul li:eq(2)').after(`<li>new</li>`)
    

    Use ' or " within braces I have used ` since it was getting formatted as html.

    0 讨论(0)
  • 2021-01-02 17:45

    Try this out:

    $('ul li:nth-child(3)').after('<li>new</li>');
    
    0 讨论(0)
提交回复
热议问题