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

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

问题描述

我有这样的代码:

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

  • 消息中心
  • ;

    我试过了:

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

    但是这会在最后一个 li(就在结束标记之前)而不是在它之后添加新的 li inside.添加此 li 的最佳方法是什么?

    解决方案

    这样做:

    $("#header ul").append('<li><a href="/user/messages"><span class="tab">消息中心</span>;</a></li>');

    两件事:

    • 您可以将
    • 附加到
        本身.
      • 您需要使用与您在 HTML 中使用的引号类型相反的引号.因此,由于您在属性中使用双引号,因此请用单引号将代码括起来.

      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?

      解决方案

      This would do it:

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

      Two things:

      • 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天全站免登陆