html,
<div id="first">
<a>Come here</a>
</div>
<div id=second">
second div
</div>
CSS,
#first a:hover{
color:green;
/*i want to do this when hover */
#second{
background:green;
}
}
在这里,如果用户光标转到“来到这里”,我想更改其他元素#second 的背景颜色。
仅使用 css 是否可行?还是我必须使用 jquery 或 javascript 事件?
最佳答案
#first:hover + #second{
background: green;
}
http://jsfiddle.net/DerekL/8rJmC/
如果 #second
不是直接在 #first
之后,则使用 ~
。
请注意,仅使用 CSS 不可能将 :hover
事件附加到 a
上。上面的代码将其附加到 #first
。
如果你真的必须将它附加到 a
,你可以用 jQuery 这样做:
$("#first > a").hover(function(){
$("#second").css(...);
}, ....);
关于javascript - 悬停时我可以处理其他元素吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20941570/