jQuery 两个 mouseenter 事件,触发一个函数一次
我将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用命名空间事件尝试此操作,该事件仅删除具有命名空间的特定事件处理程序。
Try this using namespaced events which remove only that specific event handler with namespace.
这应该可行:
不知道您是否要将其绑定到
mouseleave
或mouseenter
事件。更新:它似乎在jQuery 1.7中正常工作,但在jQuery 1.7.1。这似乎是一个错误。
This should work:
Don't know if you want to bind it to the
mouseleave
ormouseenter
event.Update: It appears to work properly in jQuery 1.7 but not in jQuery 1.7.1. It seems to be a bug.