How to add
  • in an existing
      ?
  • 2019-01-02 16:40发布

    I have code that looks like this:

    <div id="header">
        <ul class="tabs">
            <li><a href="/user/view"><span class="tab">Profile</span></a></li>
            <li><a href="/user/edit"><span class="tab">Edit</span></a></li>
        </ul>
    </div>
    

    I'd like to use jQuery to add the following to the list:

    <li><a href="/user/messages"><span class="tab">Message Center</span></a></li>
    

    I tried this:

    $("#content ul li:last").append("<li><a href="/user/messages"><span class="tab">Message Center</span></a></li>");
    

    But that adds the new li inside the last li (just before the closing tag), not after it. What's the best way to add this li?

    12条回答
    与君花间醉酒
    2楼-- · 2019-01-02 17:08

    This is another one

    $("#header ul li").last().html('<li> Menu 5 </li>');
    
    查看更多
    浮光初槿花落
    3楼-- · 2019-01-02 17:12

    You should append to the container, not the last element:

    $("#content ul").append('<li><a href="/user/messages"><span class="tab">Message Center</span></a></li>');
    

    The append() function should've probably been called add() in jQuery because it sometimes confuses people. You would think it appends something after the given element, while it actually adds it to the element.

    查看更多
    旧人旧事旧时光
    4楼-- · 2019-01-02 17:17

    easy

    // Creating and adding an element to the page at the same time.
    $( "ul" ).append( "<li>list item</li>" );
    
    查看更多
    伤终究还是伤i
    5楼-- · 2019-01-02 17:18

    How about using "after" instead of "append".

    $("#content ul li:last").after('<li><a href="/user/messages"><span class="tab">Message Center</span></a></li>');
    

    ".after()" can insert content, specified by the parameter, after each element in the set of matched elements.

    查看更多
    零度萤火
    6楼-- · 2019-01-02 17:19

    You can do it also in more 'object way' and still easy-to-read:

    $('#content ul').append(
        $('<li>').append(
            $('<a>').attr('href','/user/messages').append(
                $('<span>').attr('class', 'tab').append("Message center")
    )));    
    

    You don't have to fight with quotes then, but must keep trace of braces :)

    查看更多
    君临天下
    7楼-- · 2019-01-02 17:19

    jQuery comes with the following options which could fulfil your need in this case:

    append is used to add an element at the end of the parent div specified in the selector:

    $('ul.tabs').append('<li>An element</li>');
    

    prepend is used to add an element at the top/start of the parent div specified in the selector:

    $('ul.tabs').prepend('<li>An element</li>');
    

    insertAfter lets you insert an element of your selection next after an element you specify. Your created element will then be put in the DOM after the specified selector closing tag:

    $('<li>An element</li>').insertAfter('ul.tabs>li:last');
    will result in:
    <li><a href="/user/edit"><span class="tab">Edit</span></a></li>
    <li>An element</li>
    

    insertBefore will do the opposite of the above:

    $('<li>An element</li>').insertBefore('ul.tabs>li:last');
    will result in:
    <li>An element</li>
    <li><a href="/user/edit"><span class="tab">Edit</span></a></li>
    
    查看更多
    登录 后发表回答