简体   繁体   English

如何同时使用:not和:first-child伪选择器

[英]How to use :not and :first-child pseudo selectors simultaneously

I want to select the first li without class="test" using :not and :first-child pseudoclasses. 我想使用:not:first-childclass="test"选择第一个没有 class="test" li In this example I try to set color blue to <li>Third</li> : 在此示例中,我尝试将蓝色设置为<li>Third</li>

 ul li { color:red; } ul li:not(.test):first-child { color:blue; } 
 <ul> <li class="test">First</li> <li class="test">Second</li> <li>Third</li> <li>Fourth</li> </ul> 

Why not do the below, which uses a combination of rules to filter the item you need (you can combine rules #1 and #3). 为什么不执行以下操作,该操作使用规则组合来过滤所需的项目(可以将规则1和3组合在一起)。

This has the added advantage of it not mattering what the index is of the first item with the class .test 这具有额外的优点,即与.test类的第一项的索引.test

 ul li { /* <-- select all list items */ color: red; } li:not(.test) { /* <-- select all list which arent test */ color: blue; } ul li:not(.test) ~ li { /* <-- select all list items which follow an item without test */ color: red; } 
 <ul> <li class="test">First</li> <li class="test">Second</li> <li>Third</li> <li>Fourth</li> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM