如何暂时禁用jQuery中的单击处理程序? [英] How to temporarily disable a click handler in jQuery?

查看:118
本文介绍了如何暂时禁用jQuery中的单击处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有类似以下内容来捕获按钮的click事件:

Say I have something like the following to trap the click event of a button:

$("#button_id").click(function() {
  //disable click event
  //do something
  //re-enable click event
}

如何暂时禁用按钮的单击事件,直到原始点击处理结束?我基本上将div消失后单击按钮,但如果用户快速点击按钮几次,它会在div有机会消失之前处理所有这些点击。我想去抖动按钮,这样只有第一次点击才能在div消失之前注册。

How do I temporarily disable the click event of the button until the end of the processing of the original click occurs? I basically have the div disappear after the button is clicked, but if the user clicks on the button fast several times, it processes all those clicks before the div gets a chance to disappear. I want to "debounce" the button so that only the first click gets registered before the div disappears.

推荐答案

我注意到这篇文章很老但是它似乎在Google上排名第一,这种解决方案从未提供过所以我决定无论如何都要发布它。

I noticed this post was old but it appears top on google and this kind of solution was never offered so I decided to post it anyway.

您可以暂停游标事件并稍后通过css再次启用它们。所有主流浏览器都支持它d在某些情况下可能有用。

You can just disable cursor-events and enable them again later via css. It is supported on all major browsers and may prove useful in some situations.

$("#button_id").click(function() {

   $("#button_id").css("pointer-events", "none");
   //do something
   $("#button_id").css("pointer-events", "auto");
}

这篇关于如何暂时禁用jQuery中的单击处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆