3

我正在关注这个线程来禁用/启用单击按钮, 在 jQuery 中删除事件处理程序的最佳方法?

它的想法是你也可以使用 live() 方法来启用/禁用事件。这段代码会发生什么,当你点击一个标签元素时,例如,.load-item,它会将禁用的类添加到这个元素。这将使选择器不再与元素匹配,并且在删除“禁用”类使 .live() 选择器再次有效之前不会触发事件。

下面是我得出的结果,它确实禁用了第一个按钮的点击,

$('.load-item:not(.disabled)').live('click',function(){


/* add a .current class to the target item */
$(this).parentsUntil('.column-global').addClass('current');

/* add .disabled class to each a tag in the .current element-div */
$('.current a').each(function(index) {
    $(this).addClass('disabled');
});

....

});

当您单击另一个按钮时,我还有另一个功能可以删除禁用类,

$('.button-return').click(function(){

    ...

    /* remove all the .disabled class if any exists */
    $('.current a').each(function(index) {
        $(this).removeClass('disabled');
    });

    ...

    /* remove all the .current class if any exists */
    $('.item-global').removeClass('current');



    return false;

});

我已经检查过它确实删除了禁用类,但是第一个按钮仍然没有回到原来的可点击状态。

我错过了什么 - 还是 live() 实际上只存在一次?如果 live() 不是我应该解决的问题,还有什么更好的解决方案?

谢谢。

4

2 回答 2

1

您的返回按钮也必须是实时事件

$('.button-return:not(disabled)').live("click",function(){...});

此外,当使用 addClass() 和 removeClass() 时,您不需要在选择器上使用每个。就叫吧。

$('.current a').each(function(index) {
    $(this).removeClass('disabled');
});

和这个一样

$('.current a').removeClass('disabled');

已编辑

请注意,当您将实时事件添加到返回按钮时,您需要验证它是否未被禁用。

它完美地工作在这里是一个简单的演示。 http://www.jsfiddle.net/Qax3n/

于 2010-11-04T02:32:14.640 回答
0

它必须与您的 HTML 布局或其他 Javascript 相关。以下作品:http: //jsfiddle.net/EEJEh/

于 2010-11-04T02:56:33.987 回答