jquery 链接标签启用禁用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1169625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery link tag enable disable
提问by RaYell
I want to disable the link during loading, for the code given below
我想在加载过程中禁用链接,对于下面给出的代码
<span id="addlink">"<%= f.add_associated_link('Add Task', @project.tasks.build, :class=>"add") %></span>
I tried it with the codes below but it didn't work
我用下面的代码试过了,但没有用
$("#addlink").attr("disabled", "disabled");
and
和
$("a.add").hide();
回答by RaYell
function disableLink(e) {
// cancels the event
e.preventDefault();
return false;
}
When you want to disable it yo call
当您想禁用它时,请致电
$('#addlink').bind('click', disableLink);
When you want to enable disabled link you call
当你想启用禁用的链接时,你调用
$('#addlink').unbind('click', disableLink);
回答by rahul
$('#addlink').click(function(e) {
e.preventDefault();
//do other stuff when a click happens
});
return false;
return false;
will prevent the default event from occuring and and also prevent the event from bubbling up
将防止默认事件发生,并防止事件冒泡
So chosing between these two depends on your use. If you want to stop the default action and also need to bubble up the event then use preventDefault
因此,在这两者之间进行选择取决于您的用途。如果要停止默认操作并且还需要冒泡事件,请使用 preventDefault
回答by rahul
I'd go with a hybrid of RaYell's and phoenix's solution, adding jQuery's namespacingto the mix:
我会混合使用 RaYell 和 phoenix 的解决方案,将jQuery 的命名空间添加到组合中:
$('#addlink').bind('click.killlink',function(event){
event.preventDefault();
// You can do any additional onClick behavior here
});
To unbind this event, as well as any other related events (of any type) that you group with the .killink namespace, you'd run this:
要取消绑定此事件,以及您使用 .killlink 命名空间分组的任何其他相关事件(任何类型),您可以运行以下命令:
$('#addlink').unbind('.killlink');
As phoenix pointed out, using return false
will prevent the event from bubbling up. preventDefault()
has the added benefit of being extremely explicit (unlike return false
, which can mean many different things depending on the context).
正如 phoenix 所指出的,使用return false
将防止事件冒泡。preventDefault()
具有非常明确的额外好处(不像return false
,根据上下文可能意味着许多不同的东西)。