我有按钮,当它们悬停时它们会改变颜色。但是我试图让一个按钮保持改变后的颜色,直到另一个按钮悬停。我读了一篇帖子,它说使用a:focus但是这个实现只有在点击按钮时才有效,而不是鼠标悬停。
任何帮助表示感谢。
答案 0 :(得分: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
});
答案 1 :(得分: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";
}