我正在关注这个线程来禁用/启用单击按钮, 在 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() 不是我应该解决的问题,还有什么更好的解决方案?
谢谢。