【问题标题】:Style last children of last child样式最后一个孩子的最后一个孩子
【发布时间】:2013-09-24 03:12:06
【问题描述】:

我有以下 HTML:

<section></section>

<section id="top">
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
    </ul>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
    </ul>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>   <!-- STYLE THIS -->
        <li>5</li>   <!-- STYLE THIS -->
    </ul>
</section>

<section></section>

我已经动态生成了 HTML,我该如何设置上面两个东西的样式?使用id="top" 将样式应用于section,并在最后一个ul 中设置最后2 个list-items 的样式。

【问题讨论】:

    标签: javascript html css css-selectors


    【解决方案1】:

    这样的?

    #top > ul:last-of-type li:nth-last-child(-n+2) {
        background:aqua;
    }
    

    jsFiddle here

    使用组合选择器 - :last-of-type:nth-last-child

    注意 - 这是 IE8 及以下版本的 not fully supported

    【讨论】:

    • 需要注意的是这些使用css3选择器在9以下的IE中不可用。
    【解决方案2】:

    怎么样:

    #top > ul:nth-child(3) > li:nth-child(4),
    #top > ul:nth-child(3) > li:nth-child(5) {}
    

    Working demo

    【讨论】:

      【解决方案3】:
      #top {
          color:red;
      }
      
      #top ul:last-of-type li:last-of-type {
          color:green;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-09
        • 2011-04-18
        • 1970-01-01
        • 2013-03-12
        • 1970-01-01
        • 2022-11-10
        • 2019-02-07
        • 2011-11-08
        相关资源
        最近更新 更多