在悬停时切换css颜色(toggle a css color on hover)

编程入门 行业动态 更新时间:2024-10-19 06:17:10
在悬停时切换css颜色(toggle a css color on hover)

我正在尝试为这样的类添加高亮颜色

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

这有效,但为什么不这样做

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

当我将鼠标悬停在此时没有任何反应

当他们离开悬停时,是否没有移除来删除班级hover_me

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

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

最满意答案

在某些情况下,鼠标进入/离开事件发生了两次,这有效的,所以它正在工作,但它是每次进/出的双重切换,所以没有净效果......现在,为了安全起见:

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

这是一个错误(我相信 ),因为.live()更改支持.hover() ,这会导致一些不必要的副作用,你可以像上面那样明确100%安全,所以如果每个处理程序运行多个时间,至少为了你的目的,没关系。

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.

更多推荐

本文发布于:2023-08-01 10:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:颜色   css   toggle   hover   color

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!