jQuery:绑定和取消绑定实时点击事件
因此,我的问题有两个限制:
- 我必须在单击事件中使用外部函数调用,并且
- 必须使用实时单击事件,而不是绑定典型的单击事件。
所以我的问题是,我试图在点击事件发生后取消绑定,然后在点击事件代码完成后重新绑定它。我这样做是为了防止代码当前正在处理时重复单击(我有淡入/淡出动画,允许快速单击按钮两次或三次,从而执行我的代码 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:
- I must use an external function call in my click event, and
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据文档:
$(this)
不是选择器。According to the documentation:
$(this)
is not a selector.要解除所有使用 .live() 绑定的点击处理程序的绑定,请使用 .die() 方法:
To unbind the click handlers from all that were bound using .live(), use the .die() method:
您可以使用此模式取消绑定单击的元素并让所有其他元素存活:
与 JQUERY 1.8.2 一起使用
You can use this pattern to unbind the clicked element and let all others live:
works with JQUERY 1.8.2