简体   繁体   English

我该如何选择CSS?

[英]How would I select this with CSS?

Lets say I have the following.. 可以说我有以下内容。

<div>
    <div>
        <div>
            <div></div>  <<<Select this one..
            <div></div>  <<<Not this one..
            <div></div>  <<<Select this one..
            <div></div>  <<<Select this one..
        </div>
    </div>
</div>

How would I select those 3 divs without adding any classes or ids? 如何在不添加任何类或ID的情况下选择这3个divs Is this even possible? 这有可能吗?

You can use the :not() and :nth-child() pseudo-classes. 您可以使用:not():nth-​​child()伪类。

 div > div > div > div:not(:nth-child(2)){ color: red; } 
 <div> <div> <div> <div>Test</div> <div>Test</div> <div>Test</div> <div>Test</div> </div> </div> </div> 

Demo in jsFiddle jsFiddle中的演示

Note : For ie8 support , you could use the same selector in jQuery and style your element that way. 注意 :对于ie8支持 ,您可以在jQuery中使用相同的选择器,并以这种方式设置元素的样式。

 $("div > div > div > div:not(:nth-child(2))") .css("background-color", "yellow") 
 <div> <div> <div> <div>Test</div> <div>Test</div> <div>Test</div> <div>Test</div> </div> </div> </div> <!-- External Resources --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

If you need to support IE7 you can use: 如果需要支持IE7,可以使用:

div > div > div > div + div + div, 
div > div > div > div:first-child {
    color: orange;
}

http://jsfiddle.net/4TYcb/1/ http://jsfiddle.net/4TYcb/1/

div div div :not(:nth-child(2))

只会选择那些div

If you need a solution that also works for older IEs you have to think a bit different than supposed by most of the answers so far. 如果您需要一种适用于较旧IE的解决方案,则必须考虑与迄今为止大多数答案所假定的有所不同。

Style all DIVs that are the third child: 设置所有第三个子DIV的样式:

div > div > div > div {/* styles for all 4 DIVs */}

And then change the second one back: 然后改回第二个:

div > div > div > div:first-child + div {/* styles only for the second DIV */}

See jsFiddle jsFiddle

But as already written in my comment on your question, your HTML markup is highly "questionable". 但是,正如我在对您的问题的评论中所写的那样,您的HTML标记非常“有问题”。

PS: The difference to @Adrift's answer is, that the second rule selects the second DIV. PS:与@Adrift答案的区别在于,第二条规则选择了第二个DIV。 IMHO this is more useful in practice rather than to exclude an element. 恕我直言,这在实践中比排除一个元素更有用。 It is also "more robust" just in case another element than a DIV will be inserted. 万一将插入除DIV之外的其他元素,它也“更强大”。

As you just wrote that this is not what you are looking for, here is another option to achieve " what you are looking for "! 正如您刚刚写的那样,这不是您要寻找的东西,这是实现“ 您要寻找的东西 ”的另一种选择!

In your case you could also use the :nth-of-type() selector, instead of the :nth-child() one - see Fiddle 在您的情况下,您也可以使用:nth-of-type()选择器,而不是:nth-child()选择器-请参阅Fiddle

As you see there is a whole bunch of options. 如您所见,这里有很多选择。 You should take the time and try to understand all these different approaches to be able to decide which one will fit your needs best! 您应该花时间尝试理解所有这些不同的方法,以便能够确定哪种方法最适合您的需求!

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

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