我如何“重新绑定”解除绑定后的点击事件(“点击”)?
我有一个锚标记 next
制成“按钮”。有时,如果没有新内容可显示,则需要隐藏此标签。如果我只是用 .hide() 隐藏按钮并用 .show() 重新显示它,那么一切都可以正常工作。但我想使用 .fadeIn() 和 .fadeOut() 代替。
我遇到的问题是,如果用户在淡出动画期间单击按钮,可能会导致我运行节目的逻辑出现问题。我找到的解决方案是在原来的点击功能开始后,将点击事件与按钮解除绑定,然后在动画完成后重新绑定。
$('a.next').click(function() {
$(this).unbind('click');
...
// calls some functions, one of which fades out the a.next if needed
...
$(this).bind('click');
}
上面例子的最后一部分不起作用。点击事件实际上并没有重新绑定到锚点。有谁知道完成此操作的正确方法?
我是一个自学成才的jquery人员,所以一些更高级别的东西,比如unbind()和bind(),超出了我的理解范围,而且jquery文档对于我来说还不够简单,无法理解。
I have an anchor tag <a class="next">next</a>
made into a "button". Sometimes, this tag needs to be hidden if there is nothing new to show. All works fine if I simply hide the button with .hide() and re-display it with .show(). But I wanted to uses .fadeIn() and .fadeOut() instead.
The problem I'm having is that if the user clicks on the button during the fadeOut animation, it can cause problems with the logic I have running the show. The solution I found was to unbind the click event from the button after the original click function begins, and then re-bind it after the animation is complete.
$('a.next').click(function() {
$(this).unbind('click');
...
// calls some functions, one of which fades out the a.next if needed
...
$(this).bind('click');
}
the last part of the above example does not work. The click event is not actually re-bound to the anchor. does anyone know the correct way to accomplish this?
I'm a self-taught jquery guy, so some of the higher level things like unbind() and bind() are over my head, and the jquery documentation isn't really simple enough for me to understand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只想添加一个检查,看看它是否首先动画:
I would just add a check to see if it's animating first:
当您重新绑定点击事件时,您将从头开始。您需要提供您打算处理事件的函数。在您调用“取消绑定”后,jQuery 不会为您记住它们。
在这个特殊情况下,情况有点复杂。问题是,当您启动这些动画时,您将启动一系列由计时器驱动的动作。因此,在点击处理程序本身内部取消绑定并重新绑定“点击”处理程序实际上没有任何帮助。 有帮助的(假设您不想使用
is(":animated")
技巧解决问题)是取消绑定处理程序,然后在动画中的“完成”回调。当您首先设置“click”处理程序时,您可以将函数的声明与对“click()”的调用分开:
然后,如果您最终想要取消绑定/重新绑定,您可以重复该调用“click”以同样的方式引用“handler”函数。
When you rebind the click event, you're starting all over. You need to provide the function that you intend to handle the events. jQuery doesn't remember them for you after you call "unbind."
In this particular case, it's a little more complicated. The problem is that when you start up those animations you're setting into motion a series of actions that are driven by a timer. Thus, unbinding and rebinding the "click" handler inside the click handler itself really won't help anything. What would help (assuming you don't want to solve the problem with the
is(":animated")
trick) would be to unbind the handler and then rebind in the "finish" callback from your animation.When you set up the "click" handler in the first place, you can separate the declaration of the function from the call to "click()":
Then, should you end up wanting to unbind/rebind, you can repeat that call to "click" and refer to the "handler" function the same way.
更简单且简短的解决方案
是如果要解除绑定,则向对象添加一个类,然后删除该类以重新绑定ex;
A simpler and short solution
is to add a class to the object if you want to unbind it then remove this class to rebind ex;