2010-08-06 238 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