Hed*_*ide 140 css css-selectors
我正在寻找一个css选择器,让我选择列表的前一个孩子.
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li> <!-- select the pre last item dynamically no matter how long this list is -->
<li>6</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
静态方法:
ul li:nth-child(5)
Run Code Online (Sandbox Code Playgroud)
动态方法:
ul li:last-child(-1)
Run Code Online (Sandbox Code Playgroud)
这当然不验证,也nth-last-child似乎没有提供动态方式..我可以回退到javascript但我想知道是否有一种css方式我忽略了
Bol*_*ock 264
你可以用:nth-last-child(); 事实上,除了:nth-last-of-type()我不知道你还能用什么.我不确定你的"动态"是什么意思,但如果你的意思是这个风格是否适用于新的第二个孩子,当更多的孩子被添加到列表中时,是的.互动小提琴.
ul li:nth-last-child(2)
Run Code Online (Sandbox Code Playgroud)