在悬停时切换css颜色 [英] toggle a css color on hover
问题描述
我正在尝试为这样的类添加高亮颜色
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").hover(function(){
$(".common_box").toggleClass("hover_me");
});
当我将鼠标悬停在此状态时没有任何反应
when i hover over this nothing happens
当他们离开悬停时没有卸载班级hover_me
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");
});
由于相信) =http://api.jquery.com/live/\"rel =noreferrer> .live()
更改为支持 .hover()
,这会造成一些不必要的副作用,你可以像上面那样明确100%安全,所以如果每个处理程序运行多次,至少为了你的目的,它就没问题。
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.
这篇关于在悬停时切换css颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!