Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 使用:n子项和:非子项设置css样式_Html_Css_Css Selectors - Fatal编程技术网

Html 使用:n子项和:非子项设置css样式

Html 使用:n子项和:非子项设置css样式,html,css,css-selectors,Html,Css,Css Selectors,我有一个要使用第n个子选择器设置样式的divs列表。我还希望能够排除div,如果它有某个类,即: <style> .a:not(.b):nth-child(2n) { color: hotpink; } </style> <div class="a"> Test </div> <div class="a b"> Test </div> <div class="a"> I should be pink

我有一个要使用第n个子选择器设置样式的
div
s列表。我还希望能够排除
div
,如果它有某个类,即:

<style>
 .a:not(.b):nth-child(2n) {
    color: hotpink;
 }
</style>

<div class="a"> Test </div>
<div class="a b"> Test </div>
<div class="a"> I should be pink, as i am the 2nd child that doesnt have a "b" class </div>
<div class="a"> Test </div>
<div class="a"> Test </div>
<div class="a"> Test </div>
<div class="a"> Test </div>
<div class="a"> Test </div>
<div class="a"> Test </div>

.a:非(.b):第n个子项(2n){
颜色:粉红;
}
试验
试验
我应该是粉红色的,因为我是第二个没有“b”级的孩子
试验
试验
试验
试验
试验
试验

我认为最好的方法是使用jQuery和两个filter()调用,如:

$('.a').filter(function(){
        return !$(this).hasClass('b');
     }).filter(
    function(i){
        return (i+1)%2 == 0; 
    }
).css('color','hotpink');

:not和:nth child没有我们希望的那么灵活(很遗憾)

也许有必要确切描述一下您正试图通过此实现什么,因为可能有一种更简单的方法。如果有一个选项的话,使用javascript也很容易。据我所知,这在当前的CSS伪类中是不可能的
nth child
和类似的选择器关闭了它们的子索引,而不是使用
.x
类的子索引。实际上,我正在尝试设置Windows 8列表视图的样式,即当鼠标落在某个项目上时,它会注入一个隐藏的div,这反过来会重新为列表的其余部分着色。似乎是要走的路,真的吗