jQuery 两个 mouseenter 事件,触发一个函数一次

发布于 2024-12-28 15:15:13 字数 780 浏览 6 评论 0原文

我将 mouseenter/mouseleave 事件绑定到一个元素(只是一个图像),该元素依次执行两个函数。我的问题是我希望这两个函数中的一个只在第一个 mouseenter 上触发一次。

也就是在第一个 mouseenter 上发生两件事,在第二个 mouseneter 上只发生一件事。

$(.bubbles).mouseenter(function(){
    functionSwapImage();                         //this should happen everytime
}).mouseleave(function(){
    functionSwapImageBack();                     //this should happen everytime
    $(".bubbles").one("mouseleave", function(){
        myFunction();                            //this should happen once!
    });
});

编辑: http://jsfiddle.net/Am5ZC/

如果我移动 .one 函数在该块之外并进入其自己单独的鼠标输入/离开块,则 functionSwapImageBack() 仅触发一次(即 .one 正在删除两个代码块的事件)。

不太确定如何解决这个问题,但感谢您的帮助!

I bound a mouseenter/mouseleave event to an element (just an image), which in turn executes two functions. My issue is that I want one of the two functions to ONLY fire once on the first mouseenter.

aka on the first mouseenter two things happen, on the second mouseneter only one thing happens.

$(.bubbles).mouseenter(function(){
    functionSwapImage();                         //this should happen everytime
}).mouseleave(function(){
    functionSwapImageBack();                     //this should happen everytime
    $(".bubbles").one("mouseleave", function(){
        myFunction();                            //this should happen once!
    });
});

EDIT: http://jsfiddle.net/Am5ZC/

If I move the .one function outside of that block and into its own separate mouse enter/leave block, then the functionSwapImageBack() only fires once (ie, .one is removing the event for both blocks of code).

Not really sure how to go about this but thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

却一份温柔 2025-01-04 15:15:13

使用命名空间事件尝试此操作,该事件仅删除具有命名空间的特定事件处理程序。

$(".bubbles").mouseenter(function(){
    functionSwapImage();                         //this should happen everytime
}).mouseleave(function(){
    functionSwapImageBack();                     //this should happen everytime
}).one("mouseleave.onlyonce", function(){
    myFunction();                            //this should happen once!
});

Try this using namespaced events which remove only that specific event handler with namespace.

$(".bubbles").mouseenter(function(){
    functionSwapImage();                         //this should happen everytime
}).mouseleave(function(){
    functionSwapImageBack();                     //this should happen everytime
}).one("mouseleave.onlyonce", function(){
    myFunction();                            //this should happen once!
});
栀子花开つ 2025-01-04 15:15:13

这应该可行:

$('.bubbles').one('mouseleave', myFunction)
.mouseenter(functionSwapImage)
.mouseleave(functionSwapImageBack);

不知道您是否要将其绑定到 mouseleavemouseenter 事件。

更新:它似乎在jQuery 1.7中正常工作,但在jQuery 1.7.1。这似乎是一个错误。

This should work:

$('.bubbles').one('mouseleave', myFunction)
.mouseenter(functionSwapImage)
.mouseleave(functionSwapImageBack);

Don't know if you want to bind it to the mouseleave or mouseenter event.

Update: It appears to work properly in jQuery 1.7 but not in jQuery 1.7.1. It seems to be a bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文