2010-08-06 237 views
0

我有按钮,它们在盘旋时会改变颜色。但是我试图让一个按钮在被徘徊直到另一个按钮被徘徊后保持着改变的颜色。我读过一篇文章,并说它使用了:focus,但这是一个只在单击按钮时才起作用的实现,而不是用于鼠标悬停的事情。改变颜色悬停

任何帮助表示赞赏。

回答

2
html: 
<a class="test" href="#" onmouseover="changeColor(this);">test</a> 
<a class="test" href="#" onmouseover="changeColor(this);">test2</a> 

js/jquery: 
function changeColor(obj) { 
    $('.test').css({background:"none"}); 
    obj.style.backgroundColor="green"; 
} 
+0

好的,这很简单,谢谢。 我明白功能和所有,并且我更好地理解如何编写函数。 – 2010-08-07 00:34:20

4

这里是如何做到这一点jQuery中:

$('.button').mouseover(function(event) { // mouseOver event on all buttons with class .button 
    $('.button').css({background:"green"}); // reset all buttons' color to default green 
    $(event.target).css({background:"red"}); // change current button color to red 
}); 
+0

会不会使用'$(本)的.css({背景: “红色”});'比使用'$更好( event.target).css({background:“red”});'? – Potherca 2012-09-04 20:23:58