我如何“重新绑定”解除绑定后的点击事件(“点击”)?

发布于 2024-08-28 04:21:50 字数 614 浏览 8 评论 0原文

我有一个锚标记 制成“按钮”。有时,如果没有新内容可显示,则需要隐藏此标签。如果我只是用 .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 技术交流群。

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

发布评论

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

评论(3

平安喜乐 2024-09-04 04:21:50

我只想添加一个检查,看看它是否首先动画:

$('a.next').click(function() {
    if (!$(this).is(":animated")) {
        // do stuff
    }
});

I would just add a check to see if it's animating first:

$('a.next').click(function() {
    if (!$(this).is(":animated")) {
        // do stuff
    }
});
开始看清了 2024-09-04 04:21:50

当您重新绑定点击事件时,您将从头开始。您需要提供您打算处理事件的函数。在您调用“取消绑定”后,jQuery 不会为您记住它们。

在这个特殊情况下,情况有点复杂。问题是,当您启动这些动画时,您将启动一系列由计时器驱动的动作。因此,在点击处理程序本身内部取消绑定并重新绑定“点击”处理程序实际上没有任何帮助。 有帮助的(假设您不想使用 is(":animated") 技巧解决问题)是取消绑定处理程序,然后在动画中的“完成”回调。

当您首先设置“click”处理程序时,您可以将函数的声明与对“click()”的调用分开:

var handler = function() {
  $(this).whatever();
};
$('a.next').click(handler);

然后,如果您最终想要取消绑定/重新绑定,您可以重复该调用“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()":

var handler = function() {
  $(this).whatever();
};
$('a.next').click(handler);

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.

愁以何悠 2024-09-04 04:21:50

更简单且简短的解决方案

(不使用绑定/取消绑定方法)

是如果要解除绑定,则向对象添加一个类,然后删除该类以重新绑定ex;

$('#button').click( function(){
   if($(this).hasClass('unbinded')) return;
   //Do somthing
 });

$('#toggle').click(function(){
    $('#button').toggleClass('unbinded');
 });

A simpler and short solution

(without using the bind/unbind method)

is to add a class to the object if you want to unbind it then remove this class to rebind ex;

$('#button').click( function(){
   if($(this).hasClass('unbinded')) return;
   //Do somthing
 });

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