CSS:悬停在其他元素上?

Sve*_*ven 10 css hover

简短的问题:为什么background-color.b时候我将鼠标悬停不会改变?.a

CSS

.a {
    color: red;
}

.b {
    color: orange;
}

.a:hover .b {
    background-color: blue;
}
Run Code Online (Sandbox Code Playgroud)

HTML

<div id="wrap">
    <div class="a">AAAA</div>
    <div class ="b">BBBB</div>
</div>
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/2NEgt/

Ana*_*Ana 46

你需要.a:hover + .b而不是.a:hover .b

.a:hover .b 适用于像这样的结构

<div class="a">AAAA
  <div class ="b">BBBB</div>
</div>
Run Code Online (Sandbox Code Playgroud)

如果在某些时候你需要有.A和.B之间的一些元素,那么你就需要使用.a:hover ~ .b,它适用于所有的兄弟姐妹.a后,它的到来,不仅是下一个.

演示http://jsfiddle.net/thebabydino/EajKf/


Sta*_*ano 6

你可以使用+选择器

.a:hover + .b {
    background-color: blue;
}
Run Code Online (Sandbox Code Playgroud)

将css应用于兄弟元素,或者

.a:hover > .b {
    background-color: blue;
}
Run Code Online (Sandbox Code Playgroud)

对于嵌套类.