jQuery悬停一次? [英] jquery hover once?

查看:87
本文介绍了jQuery悬停一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使悬停函数执行一次然后停止的jQuery方法是什么?

What's the jquery way to make hover function execute once and then stop?

.one()无效..

.one() didnt work..

$(".button-color-2").hover(
  function (){
   dosmth(); 
});

谢谢

推荐答案

悬停绑定鼠标进入鼠标离开事件,这不是事件在其自己的.因此,为了具有与悬停相同的效果,您将具有两个绑定,这些绑定将触发一次.

Hover binds the handlers for Mouse Enter and Mouse Leave event and is not an event on its own. Hence, in order to have the same effect as Hover, you will have two binds that will trigger once.

$(".button-color-2").one("mouseenter mouseleave", function(e){
dosmth();
});

如果要在mouseenter和mouseleave上执行不同的操作,请绑定两个不同的处理程序

If you want to do different things on mouseenter and mouseleave, then bind two different handlers

$(".button-color-2").one("mouseenter", function(e){
dosmth1();
}).one("mouseleave", function(e){
dosmth2();
});

另一种选择是使用悬停",然后在完成后取消绑定.

The other option would be to use Hover and then unbind it once you are done.

$('.button-color-2').hover(function() {
dosmth();
$(this).unbind('mouseenter mouseleave')
});

这篇关于jQuery悬停一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆