加号选择器(+)用于选择下一个相邻同级。

前一个兄弟姐妹是否有同等的?


当前回答

我遇到了一个类似的问题,发现所有这类问题都可以通过以下方式解决:

给你的所有物品一种风格。为所选项目指定样式。使用+或~为下一项指定样式。

这样,您就可以设置当前项、先前项(所有项都被当前项和下一项覆盖)以及下一项的样式。

例子:

/* all items (will be styled as previous) */
li {
  color: blue;
}

/* the item i want to distinguish */
li.milk {
  color: red;
}

/* next items */
li ~ li  {
  color: green;
}


<ul>
  <li>Tea</li>
  <li class="milk">Milk</li>
  <li>Juice</li>
  <li>others</li>
</ul>

希望这对某人有所帮助。

其他回答

我解决了这个问题,将元素放在一个柔性框中,然后使用flex direction:column reverse。

然后,我不得不手动反转HTML中的元素(以相反的顺序排列),这看起来很正常,而且很有效!

<div style="display: flex; flex-direction: column-reverse">
  <a class="element2">Element 2</a>
  <a class="element1">Element 1</a>
</div>
...
<style>
  .element2:hover + element1 {
    ...
  }
</style>

没有“前一选择器”,但可以使用:not和~(“后选择器”)的组合。没有逆序,没有javascript。

.父级a{颜色:蓝色}.父项a.活动{颜色:红色}.parent a:不是(.parent a.active~a){颜色:红色}<div class=“parent”><a href=“”>链接</a><a href=“”>链接</a><a href=“”class=“active”>链接</a><a href=“”>链接</a><a href=“”>链接</a></div>

我认为我的方法比“设置所有div的样式,而不是删除div之后的样式”,或者使用javascript,或者使用相反的顺序更直接。

我的要求是在@Quentin的回答帮助下选择当前悬停的项目的前两个兄弟姐妹,我选择了前两个姐妹。

.儿童{宽度:25px;高度:25px;}.child:悬停{背景:蓝色;}.child:has(+.child:悬停){背景:黄色;}.child:has(+.child+.child:悬停){背景:绿色;}.child:悬停+.child{背景:红色;}.child:悬停+.child+.child{背景:洋红色;}<ul class=“parent”><li class=“child”></li><li class=“child”></li><li class=“child”></li><li class=“child”></li><li class=“child”></li><li class=“child”></li></ul>

选择所有以前的同级

.儿童{宽度:25px;高度:25px;}.child:悬停{背景:蓝色;}.child:has(~.child:悬停){背景:红色;}<ul class=“parent”><li class=“child”></li><li class=“child”></li><li class=“child”></li><li class=“child”></li><li class=“child”></li><li class=“child”></li></ul>

虽然没有以前的CSS选择器。我找到了一个快速而简单的方法来自己选择一个。以下是HTML标记:

<div class="parent">
    <div class="child-1"></div>
    <div class="child-2"></div>
</div>

在JavaScript中,只需执行以下操作:

document.querySelector(".child-2").parentElement.querySelector(".child-1")

这将首先选择父div,然后从child-2 div中选择child-1 div。

如果您使用jQuery,只需执行以下操作:

$(".child-2").prev()

不幸的是,没有“上一个”同级选择器,但您仍然可以通过使用定位(例如向右浮动)获得相同的效果。这取决于你想做什么。

在我的情况下,我想要一个主要的CSS五星评级系统。我需要为之前的星星上色(或更换图标)。通过将每个元素向右浮动,我基本上得到了相同的效果(因此,星星的html必须写为“向后”)。

我在本例中使用FontAwesome,并在fa-star-o和fa-star的unicode之间进行交换http://fortawesome.github.io/Font-Awesome/

CSS:

.fa {
    display: inline-block;
    font-family: FontAwesome;
    font-style: normal;
    font-weight: normal;
    line-height: 1;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* set all stars to 'empty star' */
.stars-container {
    display: inline-block;      
}   

/* set all stars to 'empty star' */
.stars-container .star {
    float: right;
    display: inline-block;
    padding: 2px;
    color: orange;
    cursor: pointer;

}

.stars-container .star:before {
    content: "\f006"; /* fontAwesome empty star code */
}

/* set hovered star to 'filled star' */
.star:hover:before{
    content: "\f005"; /* fontAwesome filled star code */
}

/* set all stars after hovered to'filled star' 
** it will appear that it selects all after due to positioning */
.star:hover ~ .star:before {
    content: "\f005"; /* fontAwesome filled star code */
}

HTML格式:(40)

JSFiddle:http://jsfiddle.net/andrewleyva/88j0105g/