最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable (and re-enable) the href and onclick on elements - Stack Overflow

programmeradmin3浏览0评论

I just want to enable / disable onclick and href on elements (a or div).

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.

 
$(this).unbind().click(function(event){
     event.preventDefault();
     return;
});

Edit FOUND A HACK FOR A ELEMENTS


if ($(this).attr("href")) {
     $(this).attr("x-href", $(this).attr("href"));
     $(this).removeAttr("href");
}

I just want to enable / disable onclick and href on elements (a or div).

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.

 
$(this).unbind().click(function(event){
     event.preventDefault();
     return;
});

Edit FOUND A HACK FOR A ELEMENTS


if ($(this).attr("href")) {
     $(this).attr("x-href", $(this).attr("href"));
     $(this).removeAttr("href");
}
Share Improve this question edited Dec 5, 2022 at 19:27 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Jan 7, 2011 at 13:26 jibeesjibees 5462 gold badges4 silver badges17 bronze badges 2
  • It's a pretty vague question. What is it exactly you're trying to do? Remove the href property as well? – Klemen Slavič Commented Jan 7, 2011 at 13:33
  • 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) – jibees Commented Jan 7, 2011 at 13:40
Add a comment  | 

5 Answers 5

Reset to default 6

If you return false on the onclick event, the href is irgnored.

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

  2. This will not go to Google: <a href="http://www.google.com" onclick="alert('Go to Google'); return false;">Test</a>

Ok i've found a workaround : putting an overlay over the main div containing all the elements i wanted to disable .. It just works.

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.

SO question: JavaScript event.preventDefault vs return false.

I'm unsure as to the problem of the "href still being available," since the click event is cancelled; however if you want to remove the href from a elements:

$('a[href]').attr('href','#');

will remove them (or, rather, replace the URL with a #).


Edited in response to comment (to question) by 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)

$('#buttonRemoveClickId, .buttonClassName').click(
function() {
    $('a, div').unbind('click');
});
$('#buttonReplaceClickId, .buttonOtherClassName').click(
function() {
    $('a, div').bind('click');
});
  • unbind(),
  • bind().

Try this to disable click:

$(this).unbind('click');

You can set the href attribute directly to "" to prevent the original from showing up in the status bar, if that's what you're asking.

$(this).unbind().click(function(event){
     event.preventDefault();
}).attr("href", "");

Otherwise, a event.preventDefault() already stops links from being clickable.

发布评论

评论列表(0)

  1. 暂无评论