为最后一个孩子留下最后的孩子

0 html javascript css css-selectors css3

我有以下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>
Run Code Online (Sandbox Code Playgroud)

我有动态生成的HTML,我如何设置以上两种风格?应用样式的sectionid="top",款式最后2 list-items中的最后一个ul.

Jos*_*ier 8

像这样的东西?

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

jsFiddle在这里

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

注意 - IE8并没有完全支持这一点.

  • 需要注意的是这些使用css3选择器,这些选择器在9以下的IE中不可用. (3认同)