选择CSS中的相邻元素

时间:2012-12-26 11:19:50

标签: css attributes css-selectors

在CSS中,

是否可以匹配具有某个属性的元素,后跟具有另一个属性的元素,即

让我们考虑一下

<div my-custom-attribute="0">
</div>

<div my-custom-attribute="1">
</div>

我想选择my-custom-attribute等于0的所有元素,后跟my-custom-attribute等于1的元素

3 个答案:

答案 0 :(得分:1)

您可以通过属性选择器以相反的顺序执行此操作

div[my-custom-attribute="0"] > div[my-custom-attribute="1"]

<div id="parent">
    <div my-custom-attribute="0">
    </div>
    <div my-custom-attribute="1">
    </div>
</div>

#parent > div[my-custom-attribute]:not(:first-child)

答案 1 :(得分:1)

我认为这就是你想要的:

div[my-custom-attribute="0"] + div[my-custom-attribute="1"] { 
  color:red;
} 

DEMO

有关选择器的更多信息,请参阅此link

答案 2 :(得分:0)

感谢您的回答。我最终使用jQuery选择器而不是CSS。

就我而言,jQuery prev()有效

$($('div[my-custom-attribute="1"]').prev('div[my-custom-attribute="0"]'))