我有以下html
<ul class="main"> ... </ul>
<div class="submain">...</div> --> select this
<div class="submain">...</div>
<div class="submain">...</div>
...
<ul class="main"> ... </ul>
<div class="submain">...</div> --> select this
<div class="submain">...</div>
<div class="submain">...</div>
如何选择main之后的所有第一个submain元素。
答案 0 :(得分:2)
您可以这样做
在这种情况下,您需要选择该类的同级,以便可以使用+
选择器来实现。
The element+element selector is used to select elements that is placed immediately after (not inside) the first specified element.
您可以在
Adjacent sibling combinator
.main + .submain{
color:green;
background:'blue';
}
<ul class="main"> ... </ul>
<div class="submain">Selected</div> --> select this
<div class="submain">...</div>
<div class="submain">...</div>
...
<ul class="main"> ... </ul>
<div class="submain">Selected</div> --> select this
<div class="submain">...</div>
<div class="submain">...</div>
答案 1 :(得分:0)
正如Code Maniac所说-使用“ +”组合器选择相邻的兄弟姐妹。从我这里,我将为您提供CSS组合器工作方式的文档链接: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors#Combinators