如何将列表项添加到现有的无序列表中? [英] How to add a list item to an existing unordered list?

查看:108
本文介绍了如何将列表项添加到现有的无序列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下代码:

<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>

我想使用jQuery将以下内容添加到列表中:

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>

我尝试过:

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

但是,这会将新的li 内部添加到最后一个li(恰好在结束标记之前),而不是在其之后.添加此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?

推荐答案

这可以做到:

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

两件事:

  • 您可以将<li>附加到<ul>本身.
  • 您需要使用与HTML中所使用的相反的引号类型.因此,由于您在属性中使用了双引号,因此在代码中用单引号引起来.
  • You can just append the <li> to the <ul> itself.
  • You need to use the opposite type of quotes than what you're using in your HTML. So since you're using double quotes in your attributes, surround the code with single quotes.

这篇关于如何将列表项添加到现有的无序列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆