我需要在div .tGrid-row-two中选择类.tGrid-item的第一个div,但是:first-child选择我每个.tGrid-row-two的第一个t-Grid项。 / p>
我正在使用sass
代码示例:
<div class="tGrid">
<div class="tGrid-row-four">
<div class="tGrid-row-two">
<div class="tGrid-item">
this is the one I want to select
</div>
<div class="tGrid-item">
no select
</div>
</div>
<div class="tGrid-row-two">
<div class="tGrid-item">
no select
</div>
<div class="tGrid-item">
no select
</div>
</div>
</div>
<div class="tGrid-row-four">
<div class="tGrid-row-two">
<div class="tGrid-item">
this is the one I want to select
</div>
<div class="tGrid-item">
no select
</div>
</div>
<div class="tGrid-row-two">
<div class="tGrid-item">
no select
</div>
<div class="tGrid-item">
no select
</div>
</div>
</div>
</div>
&#13;
答案 0 :(得分:1)
您可以将first-child
个选择器串在一起:
.tGrid-row-two:first-child .tGrid-item:first-child { }
正如@ j08691指出的那样,只有当.tGrid-item
是.tGrid-row-two
的直接后代并且除了.tGrid-item
类的其他元素之外没有其他兄弟姐妹时,这才有效。 / p>
.tGrid-row-two:first-child .tGrid-item:first-child {
background:blue;
color:#fff;
}
&#13;
<div class="tGrid">
<div class="tGrid-row-four">
<div class="tGrid-row-two">
<div class="tGrid-item">
this is the one I want to select
</div>
<div class="tGrid-item">
no select
</div>
</div>
<div class="tGrid-row-two">
<div class="tGrid-item">
no select
</div>
<div class="tGrid-item">
no select
</div>
</div>
</div>
<div class="tGrid-row-four">
<div class="tGrid-row-two">
<div class="tGrid-item">
this is the one I want to select
</div>
<div class="tGrid-item">
no select
</div>
</div>
<div class="tGrid-row-two">
<div class="tGrid-item">
no select
</div>
<div class="tGrid-item">
no select
</div>
</div>
</div>
</div>
&#13;
答案 1 :(得分:0)
基本上,像这样:
.tGrid-row-two:first-child .tGrid-item:first-child {
color:red;
}
&#13;
<div class="tGrid">
<div class="tGrid-row-four">
<div class="tGrid-row-two">
<div class="tGrid-item">
this is the one I want to select
</div>
<div class="tGrid-item">
no select
</div>
</div>
<div class="tGrid-row-two">
<div class="tGrid-item">
no select
</div>
<div class="tGrid-item">
no select
</div>
</div>
</div>
<div class="tGrid-row-four">
<div class="tGrid-row-two">
<div class="tGrid-item">
this is the one I want to select
</div>
<div class="tGrid-item">
no select
</div>
</div>
<div class="tGrid-row-two">
<div class="tGrid-item">
no select
</div>
<div class="tGrid-item">
no select
</div>
</div>
</div>
</div>
&#13;