jquery: live() - 只存在一次?
我正在关注此线程来禁用/启用按钮的单击, 在 jQuery 中删除事件处理程序的最佳方法?
它的想法是您还可以使用 live() 方法来启用/禁用事件。此代码将发生的情况是,当您单击标记元素(例如 .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');
});
....
});
并且当您单击另一个按钮时,我有另一个函数可以删除禁用类,
$('.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() 不是我应该研究的解决方案,还有更好的解决方案吗?
谢谢。
I am following this thread to disable/ enable the click on a button,
Best way to remove an event handler in jQuery?
its idea is that you can also use the live() method to enable/disable events. What will happen with this code is that when you click on an a tag-element, for instance, .load-item, it will add the class disabled to this element. This will make it so that the selector no longer matches the element and the event will not be fired until the 'disabled' class is removed making the .live() selector valid again.
Below is what I came out with and it does disable the click on the first button,
$('.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');
});
....
});
and I have another function to remove the disabled-class when you click on another button,
$('.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;
});
I have checked that it does remove the disabled-class, but the first button still is not back to the original clickable state.
have I missed out anything - or the live() actually only lives once only? any better solutions if live() is not the one I should work on??
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的返回按钮也需要是一个实时事件此外
,当使用 addClass() 和 removeClass() 时,您不需要在选择器上使用每个按钮。只要调用它即可。
相同
与此已编辑
。请注意,当您将实时事件添加到返回按钮时,您需要验证它是否未被禁用。
它完美地工作在这里是一个简单的 演示。 http://www.jsfiddle.net/Qax3n/
Your return button needs to be a live event as well
Also when using addClass() and removeClass() you do not need to use each on the selector. Just call it.
is the same as this
Edited
Note when you add the live event to the return button you need to verify that it is not disabled.
It works perfectly here is a simple demo. http://www.jsfiddle.net/Qax3n/
它一定与你的 HTML 布局或其他 Javascript 有关。以下作品:
http://jsfiddle.net/EEJEh/
It must have something to do with your HTML layout or other Javascript. The following works:
http://jsfiddle.net/EEJEh/