jQuery:绑定和取消绑定实时点击事件

发布于 2024-08-12 15:21:23 字数 626 浏览 2 评论 0原文

因此,我的问题有两个限制:

  1. 我必须在单击事件中使用外部函数调用,并且
  2. 必须使用实时单击事件,而不是绑定典型的单击事件。

所以我的问题是,我试图在点击事件发生后取消绑定,然后在点击事件代码完成后重新绑定它。我这样做是为了防止代码当前正在处理时重复单击(我有淡入/淡出动画,允许快速单击按钮两次或三次,从而执行我的代码 2 或 3 次,这是不希望的) 。我正在使用的代码如下:

$item.live("click", handleClick);

我疯了吗

function handleClick(ev) {

    $(this).die("click");

    // perform code here, including things with 'ev'

    $(this).live("click", handleClick);
}

,或者这应该没有问题吗?现在,我可以点击一次,但之后就不能再点击了。很明显 die() 正在工作,但由于某种原因它没有被重新绑定到该函数。我已经验证它确实到达了handleClick()中的代码以重新绑定实时点击。

有什么想法吗?任何帮助将不胜感激。谢谢。

So there are two constraints to my question:

  1. I must use an external function call in my click event, and
  2. I must use a live click event, rather binding a typical click event.

So my problem is that I'm trying to unbind a click event after it occurs, and then rebind it once the click event code is complete. I'm doing this to prevent duplicate clicks while code is currently in process (I have fadeIn/Out animation that will allow the button to be clicked twice or three times rapidly, thereby executing my code 2 or 3 times, which is nto desired). The code I'm using is below:

$item.live("click", handleClick);

and

function handleClick(ev) {

    $(this).die("click");

    // perform code here, including things with 'ev'

    $(this).live("click", handleClick);
}

Am I crazy, or should this be working with no problems? Right now, I can click once, but not again afterward. So clearly the die() is working, but it's not being re-bound for some reason to that function. I have verified that it does reach the code in handleClick() to re-bind the live click.

Any ideas? Any help would be greatly appreciated. Thanks.

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

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

发布评论

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

评论(3

软糯酥胸 2024-08-19 15:21:23

根据文档

实时事件目前仅在针对选择器使用时才有效。

$(this) 不是选择器。

According to the documentation:

Live events currently only work when used against a selector.

$(this) is not a selector.

梦在深巷 2024-08-19 15:21:23

要解除所有使用 .live() 绑定的点击处理程序的绑定,请使用 .die() 方法:

$(".clickme").die("click");

To unbind the click handlers from all that were bound using .live(), use the .die() method:

$(".clickme").die("click");
错爱 2024-08-19 15:21:23

您可以使用此模式取消绑定单击的元素并让所有其他元素存活:

$('a.myselector').live('click', function() {

    // do things

    $(this).unbind('click').live('click', function() {return false;});

    return false; // block the link
});

与 JQUERY 1.8.2 一起使用

You can use this pattern to unbind the clicked element and let all others live:

$('a.myselector').live('click', function() {

    // do things

    $(this).unbind('click').live('click', function() {return false;});

    return false; // block the link
});

works with JQUERY 1.8.2

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