【发布时间】:2016-11-01 07:11:58
【问题描述】:
我想,当第一课悬停时,第二课的颜色是白色的。
<div class="diva">This is the first class</div>
<div class="divb">This is the second class</div>
<style>
.diva:hover .divb {
color:#fff;
}
</style
但是,这行不通。我该怎么做?
【问题讨论】:
我想,当第一课悬停时,第二课的颜色是白色的。
<div class="diva">This is the first class</div>
<div class="divb">This is the second class</div>
<style>
.diva:hover .divb {
color:#fff;
}
</style
但是,这行不通。我该怎么做?
【问题讨论】:
CSS 3 general sibling combinator~ 可以在您的情况下用于为另一个类提供悬停效果。
两个序列所代表的元素[
E1 ~ E2]共享同一个 文档树中的父级和第一个序列表示的元素 [E1] 在由 第二个 [E2]。
如果用户将鼠标悬停在diva 上,下面的代码将divb 的字体颜色设置为白色。如果元素不在相邻位置,则可以使用此组合器
<html>
<body>
<div class="diva">Hover over the first element</div>
<div class="divb">I will change my color</div>
<style>
.diva:hover ~ .divb {
color:#fff;
}
</style>
</html>
【讨论】:
您正在寻找adjacent sibling selector (CSS 2.1) +。引用规格
如果 E1 和 E2 在 文档树和 E1 紧接在 E2 之前
.diva:hover + .divb {
color: #fff;
background-color: green;
padding-left: 4em;
}
<div class="diva">Hover over first div to change color of sibling</div>
<div class="divb">This is the second class</div>
【讨论】: