使用JQuery禁用和启用所有超链接 [英] Disable and Enable ALL Hyperlinks using JQuery
问题描述
我在下面具有禁用所有超链接的功能,但是在发生事件后,我想再次将其全部启用,怎么办?
I have the below which disables all hyperlinks but after an event I want to enable them back all again, how can I do this?
$("a").click(function() { return false; });
我不认为将其设置为true这么简单. ;)
I don't think its as simple as just setting it to true. ;)
谢谢
推荐答案
执行此操作,而不是用这种方式绑定"click"处理程序:
Instead of binding your "click" handler that way, do this:
$('a').bind("click.myDisable", function() { return false; });
然后,当您想删除该处理程序时,很简单:
Then when you want to remove that handler it's easy:
$('a').unbind("click.myDisable");
这样,您可以避免弄乱可能绑定到点击"的其他内容.如果仅取消绑定点击",则取消绑定到该事件的所有内容.
That way you avoid messing up other stuff that might be bound to "click". If you just unbind "click", you unbind everything bound to that event.
2014年修改—现在绑定事件的方式是使用.on()
:
edit in 2014 — the way you bind events now is with .on()
:
$('a').on('click.myDisable', function() { return false; });
可能最好这样做:
$('a').on('click.myDisable', function(e) { e.preventDefault(); });
解除绑定:
$('a').off('click.myDisable');
最后,您可以将处理程序绑定到文档主体并处理动态添加的<a>
标签:
Finally, you could bind a handler to the document body and deal with <a>
tags that are dynamically added:
$('body').on('click.myDisable', 'a', function(e) { e.preventDefault(); });
// to unbind
$('body').off('click.myDisable');
这篇关于使用JQuery禁用和启用所有超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!