禁用(并重新启用)href和onclick元素

jib*_*ees 5 javascript jquery javascript-events

我只想在元素(a或div)上启用/禁用onclick和href.

我不知道该怎么做.

我可以通过在click事件上添加处理程序来禁用onclick,但href仍然可用.

 
$(this).unbind().click(function(event){
     event.preventDefault();
     return;
});
Run Code Online (Sandbox Code Playgroud)

编辑找到了一个元素


if ($(this).attr("href")) {
     $(this).attr("x-href", $(this).attr("href"));
     $(this).removeAttr("href");
}
Run Code Online (Sandbox Code Playgroud)

wos*_*sis 6

如果您返回falseonclick事件,href则会被忽略.

  1. 这将去Goole: <a href="http://www.google.com" onclick="alert('Go to Google')">Test</a>

  2. 这不会发给谷歌: <a href="http://www.google.com" onclick="alert('Go to Google'); return false;">Test</a>


jib*_*ees 5

好的,我找到了一个解决方法:在主div上放置一个叠加层,其中包含我想要禁用的所有元素..它只是有效.