【问题标题】:How to find the li.last and add css using jquery如何使用 jquery 找到 li.last 并添加 css
【发布时间】:2012-03-14 01:31:14
【问题描述】:

我有一个使用后面的代码以编程方式生成的无序列表,html 标记如下:

<ul>
<li></li>
<li>
     <ul>
        <li></li>
        <li></li>
        <li></li>
        <li>last</li>
    </ul>
</li>
<li></li>
<li>
    <ul>
       <li></li>
       <li></li>
       <li></li>
       <li>last</li>
    </ul>
</li>
</ul>

问题是 ul li ul li a 的样式为 border-bottom: solid 1px white;,我想设置 border-bottom: none 对于last sub li,是否可以使用jquery找到最后一个li然后修改类,或者 有没有更好的方法来实现这样的事情, 谢谢

【问题讨论】:

    标签: jquery jquery-selectors html-lists


    【解决方案1】:
    $('ul li ul li:last-child a').css('border-bottom', 'none');
    

    选择最后一个 li 并更改其 CSS border-bottom 值,这使用 :last-child selectorworking example

    CSS3 中,您可以执行以下操作:

    ul > li > ul > li:last-child a {
        border-bottom: none;
    }
    

    Working example using CSS3

    【讨论】:

    • @MrA 尝试在$(document).ready(function(){ // here }); 中使用它,以确保在操作之前准备好 DOM ...
    • @MrA 尝试在选择器中添加a
    • 是的,尝试了这两个,但侧面菜单仍然显示最后一个 li 的边框底部:(
    【解决方案2】:

    是的,但您可以考虑使用纯 CSS 解决方案:

    li:last-child { 
      border-bottom: none; 
    }
    

    【讨论】:

      【解决方案3】:

      在 CSS 中(不适用于 IE8)

      ul ul li:last-child { border-bottom: none; }
      

      【讨论】:

        【解决方案4】:

        这是你要找的吗?

        $("li:last-child a").css('border-bottom','none');
        

        去掉最后一个li的锚标签的下边框?

        【讨论】:

          猜你喜欢
          • 2011-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多