Javascript 禁用(并重新启用)href 和 onclick 元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4625893/
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
Disable (and re-enable) the href and onclick on elements
提问by jibees
I just want to enable / disable onclick and href on elements (a or div).
我只想在元素(a 或 div)上启用/禁用 onclick 和 href。
I don't know how to do this.
我不知道该怎么做。
I can disable onclick by adding an handler on click event, but the href is still available.
我可以通过在 click 事件上添加处理程序来禁用 onclick,但 href 仍然可用。
$(this).unbind().click(function(event){
event.preventDefault();
return;
});
EditFOUND A HACK FOR A ELEMENTS
编辑发现一个元素的黑客
if ($(this).attr("href")) {
$(this).attr("x-href", $(this).attr("href"));
$(this).removeAttr("href");
}
回答by wosis
If you return falseon the onclick event, the hrefis irgnored.
如果您返回falseonclick 事件,href则忽略该事件。
This will go to Goole:
<a href="http://www.google.com" onclick="alert('Go to Google')">Test</a>This will not go to Google:
<a href="http://www.google.com" onclick="alert('Go to Google'); return false;">Test</a>
这将转到 Goole:
<a href="http://www.google.com" onclick="alert('Go to Google')">Test</a>这不会去谷歌:
<a href="http://www.google.com" onclick="alert('Go to Google'); return false;">Test</a>
回答by jibees
Ok i've found a workaround : putting an overlay over the main div containing all the elements i wanted to disable .. It just works.
好的,我找到了一个解决方法:在包含我想禁用的所有元素的主 div 上放置一个覆盖层。它只是有效。
回答by David says reinstate Monica
You could try the following:
您可以尝试以下操作:
$('a, div').click(
function(e){
return false;
// cancels default action *and* stops propagation
// or e.preventDefault;
// cancels default action without stopping propagation
});
MDC documentation for preventDefault, jQuery documentation for event.preventDefault.
用于 的 MDC 文档preventDefault,用于event.preventDefault.
SO question: JavaScript event.preventDefaultvs return false.
所以问题:JavaScriptevent.preventDefault与return false.
I'm unsure as to the problem of the "hrefstill being available," since the clickevent is cancelled; however if you want to remove the hreffrom aelements:
我不确定“href仍然可用”的问题,因为click活动被取消了;但是,如果要删除hreffroma元素:
$('a[href]').attr('href','#');
will remove them (or, rather, replace the URL with a #).
将删除它们(或者,更确切地说,将 URL 替换为#)。
Editedin response to comment (to question) by OP:
针对 OP 的评论(对问题)进行编辑:
Ok, sorry ;) I just want to be able (by clicking on a button), to disable / enable all the links (click or href) over elements (div or a)
好的,抱歉;) 我只想能够(通过单击按钮),禁用/启用元素(div 或 a)上的所有链接(单击或 href)
$('#buttonRemoveClickId, .buttonClassName').click(
function() {
$('a, div').unbind('click');
});
$('#buttonReplaceClickId, .buttonOtherClassName').click(
function() {
$('a, div').bind('click');
});
回答by Diablo
Try this to disable click:
试试这个来禁用点击:
$(this).unbind('click');
$(this).unbind('click');
回答by David Tang
You can set the hrefattribute directly to ""to prevent the original from showing up in the status bar, if that's what you're asking.
您可以href直接将该属性设置为""以防止原件显示在状态栏中,如果这是您的要求的话。
$(this).unbind().click(function(event){
event.preventDefault();
}).attr("href", "");
Otherwise, a event.preventDefault()already stops links from being clickable.
否则,aevent.preventDefault()已经停止链接可点击。

