Disable and Enable ALL Hyperlinks using JQuery

2020-06-17 06:18发布

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; });

I don't think its as simple as just setting it to true. ;)

Thanks all

10条回答
劳资没心,怎么记你
2楼-- · 2020-06-17 06:53

Binding and unbinding takes some overhead.

A different solution would be to add a class like disabled, then use hasClass('disabled') to test and see whether or not it should return false.

$('a').addClass('disabled');
$('a').click(function() {
    if($(this).hasClass('disabled'))
        return false;
});
查看更多
祖国的老花朵
3楼-- · 2020-06-17 06:56

Try this:

// Better to use the live event handler here, performancewise
$('a').live('click', function() {... return false;});

// Now simply kill the binding like this
$('a').die('click');

bye

查看更多
▲ chillily
4楼-- · 2020-06-17 06:57

you could unbind the click handler:

$('a').unbind('click')
查看更多
Lonely孤独者°
5楼-- · 2020-06-17 06:57

Another way, if you strictly want the url to be inaccessible as well, you can remove href attribute from a tag. Something like that:

 $('a').removeAttr('href');

additionaly changing color of text and onhover cursor:

 $('a').css('color', '#333');
 $('a').hover(function () {
       $(this).css('cursor', 'not-allowed');
 );
查看更多
登录 后发表回答