选择CSS中另一个元素之后的直接第一个元素

时间:2012-05-28 10:45:12

标签: html css

我有以下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第一个?之后的元素?

2 个答案:

答案 0 :(得分:9)

您可以使用adjacent sibling combinator

#text + span {
    /* Styles */
}

这是working example

更新(见评论)

要定位第二个span,您只需将另一个相邻的兄弟组合器添加到选择器:

#text + span + span {
    /* Styles */
}

答案 1 :(得分:1)

使用+ (adjacent sibling)选择器:

textarea + span { ... }

演示:http://jsfiddle.net/ThiefMaster/ngrZz/