我在跟进这个线程,以禁用/启用按钮的单击事件为例,
最佳方法是使用jQuery删除事件处理程序?
我已经检查过了,它确实移除了disabled-class,但第一个按钮仍然没有回到原始的可点击状态。 我有遗漏什么吗?或者live()只能使用一次?如果live()不是我应该使用的方法,是否有更好的解决方案? 谢谢。
其想法是您还可以使用live()方法来启用/禁用事件。 此代码的作用是,当您单击a标签元素(例如.load-item)时,它将向此元素添加disabled类。 这将使选择器不再匹配该元素,并且事件将不会触发,直到“已禁用”类被删除,使.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');
});
....
});
我还有另一个功能,当您单击另一个按钮时,可以删除禁用类(disabled-class)。
$('.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;
});
我已经检查过了,它确实移除了disabled-class,但第一个按钮仍然没有回到原始的可点击状态。 我有遗漏什么吗?或者live()只能使用一次?如果live()不是我应该使用的方法,是否有更好的解决方案? 谢谢。
.button-return
需要是live的?我没有看到这个类在任何地方被动态操作... - Nick Craver