我有以下HTML:
<div id="form">
<form>
<textarea id="text"></textarea>
<span class="error">select this</span>
<span class="error">don't select this</span>
</form>
</div>
在CSS中,如何在span
元素之后直接选择第一个 textarea
元素,不选择另一个{{1第一个?之后的元素?
答案 0 :(得分:9)
您可以使用adjacent sibling combinator:
#text + span {
/* Styles */
}
更新(见评论)
要定位第二个span
,您只需将另一个相邻的兄弟组合器添加到选择器:
#text + span + span {
/* Styles */
}
答案 1 :(得分:1)