开发者

toggle a css color on hover

开发者 https://www.devze.com 2023-01-17 05:55 出处:网络
I am trying to add a highlight color to a class like this $(".common_box").hover(function(){

I am trying to add a highlight color to a class like this

$(".common_box").hover(function(){
    $(".common_box").addClass("hover_me");
});

This works but why doesnt this

$(".common_box"开发者_JS百科;).hover(function(){
    $(".common_box").toggleClass("hover_me");
});

When i hover over this nothing happens

Is there no unhover to put to remove the class hover_me when they move away from the hover


There's currently a bug in some situations where the mouse enter/leave events are firing twice, so it is working, but it's double toggling each in/out, so no net effect...for now, to be safe:

$(".common_box").hover(function(){
    $(this).addClass("hover_me");
}, function(){
    $(this).removeClass("hover_me");
});

This is a bug (I believe) because of the .live() changes to support .hover(), which is causing some unwanted side-effects, you can be explicit like above to be 100% safe, so if each handler runs multiple times, at least for your purposes, it's alright.


Try this

  $(".common_box").mouseover(function() {
    $(this).addClass("hover_me");
  }).mouseout(function(){
    $(this).removeClass("hover_me");
  });
0

精彩评论

暂无评论...
验证码 换一张
取 消