Jquery 在(';单击';)上不';关闭后无法工作(';单击';)

Jquery 在(';单击';)上不';关闭后无法工作(';单击';),jquery,Jquery,以下代码不起作用。它会删除禁用的类,但div不能再次单击。我的意思是这个函数只工作一次 $(document).ready(function(){ $('div.downloadbutton').on('click', function(e) { $('div.downloadbutton').off('click'); $('div.downloadbutton').not(this).addClass('disabled', {duration:500}

以下代码不起作用。它会删除禁用的类,但div不能再次单击。我的意思是这个函数只工作一次

$(document).ready(function(){

$('div.downloadbutton').on('click', function(e) {
         $('div.downloadbutton').off('click');
         $('div.downloadbutton').not(this).addClass('disabled', {duration:500});

        setTimeout(function(){
            $('div.downloadbutton').on('click');
             $('div.downloadbutton').not(this).removeClass('disabled', {duration:500});
        }, 3000);
     });
});

与其绑定/解除绑定事件处理程序,不如尝试使用一个可以控制逻辑的状态变量。大概是这样的:

var clickDisabled = false; // initial state; allow the click

$('div.downloadbutton').on('click', function(e) {
    if (!clickDisabled) {
        $('div.downloadbutton').not(this).addClass('disabled', { duration:500 });
        clickDisabled = true;
        setTimeout(function() { clickDisabled = false; }, 3000);
     };
});

您仍然需要为('click')上的
提供一个函数。
-它不仅会替换以前附加的事件处理程序。不确定您为什么被否决?你解释了这个问题,你的代码。。。人们还想要什么?@Rorymcrossan我一点也不明白。那么,我如何才能让这个例子起作用呢?/好吧,这个问题可能是愚蠢的人在这里问的。你有没有考虑过把
click
回调函数作为一个命名函数来编写,这样你就可以把它叫做
。on('click',fn)
?你今天太棒了+1.