jQuery append to bottom of list

▼魔方 西西 提交于 2021-02-18 05:39:32

问题


Can someone lend me a hand

I have this unordered list

<ul id="nav">
<li><a href="whatwedo.aspx">WHAT WE DO</a>
<ul>
    <li><a href="development.aspx">Development</a></li>
    <li><a href="marketassessment.aspx">MARKET ASSESSMENT AND CONCEPT DEVELOPMENT</a></li>
    <li><a href="planning.aspx">DEVELOPMENT PLANNING AND OVERSIGHT</a></li>
    <li><a href="preopening.aspx">PRE-OPENING OPERATIONAL SERVICES</a></li>
    <li><a href="operations.aspx">OPERATIONAL MANAGEMENT SERVICES</a></li>
    <li><a href="turnaround.aspx">TURNAROUND SERVICES</a></li>
    <li><a href="news.aspx">NEWS</a></li>
</ul>
</li>
<li><a href="ourparks.aspx">OUR PARKS</a></li>
<li><a href="contact.aspx">CONTACT US</a></li> </ul>

And I want to add a new list to the bottom of the list.

<li class="last_link"><a href="https://projects.parc-services.com" target="blank">Login</a></li>

Would I go about it by doing something like this?

$("#nav ul").prepend("<li></li>");

回答1:


If you want to add at the end use the append() method instead of prepend():

$('#nav ul').append('<li class="last_link"><a href="https://projects.parc-services.com" target="blank">Login</a></li>');

or as I prefer:

$('#nav ul').append(
    $('<li/>', {
        'class': 'last_link',
        html: $('<a/>', {
            href: 'https://projects.parc-services.com',
            target: '_blank',
            text: 'Login'
        })
    })
);



回答2:


prepend adds something at the beginning of an element. append is used to add something at the end.

$("#nav ul").append($("<li></li>").html('something'));

And if you want to add a class, or anything else you can:

$("#nav ul").append($("<li></li>").html('something')
                                  .addClass('myclass'));


来源:https://stackoverflow.com/questions/6334628/jquery-append-to-bottom-of-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!