简体   繁体   English

悬停时更改颜色

[英]change color on hover

I have buttons and they change colors when they are hovered. 我有按钮,当它们悬停时,它们会更改颜色。 But I am trying to make a button remain with a changed color after being hovered until another button is hovered. 但是我试图使一个按钮在悬停后保持颜色不变,直到另一个按钮悬停为止。 I read a post and it said to use a:focus but this is an implementation that works only when clicking a button, not with mouseover thing. 我读了一篇文章,并说使用a:focus,但这是一个仅在单击按钮时有效的实现方式,而与鼠标悬停无关。

Any help appreciated. 任何帮助表示赞赏。

Here's how to do it in jQuery: 这是在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
});
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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM