jQuery disable a link

前端 未结 17 1196
半阙折子戏
半阙折子戏 2020-11-22 14:45

Anyone know how to disable a link in jquery WITHOUT using return false;?

Specifically, what I\'m trying to do is disable the link of an item, performing

相关标签:
17条回答
  • 2020-11-22 15:39

    If you go the href route, you can save it

    To disable:

    $('a').each(function(){
        $(this).data("href", $(this).attr("href")).removeAttr("href");
    });
    

    Then re-enable using:

    $('a').each(function(){
        $(this).attr("href", $(this).data("href"));
    });
    

    In one case I had to do it this way because the click events were already bound somewhere else and I had no control over it.

    0 讨论(0)
  • 2020-11-22 15:43

    I know this isn't with jQuery but you can disable a link with some simple css:

    a[disabled] {
      z-index: -1;
    }
    

    the HTML would look like

    <a disabled="disabled" href="/start">Take Survey</a>
    
    0 讨论(0)
  • 2020-11-22 15:44

    You can remove click for link by following;

    $('#link-id').unbind('click');
    

    You can re-enable link by followings,

    $('#link-id').bind('click');
    

    You can not use 'disabled' property for links.

    0 讨论(0)
  • 2020-11-22 15:47

    I always use this in jQuery for disabling links

    $("form a").attr("disabled", "disabled");
    
    0 讨论(0)
  • 2020-11-22 15:47

    html link example:

            <!-- boostrap button + fontawesome icon -->
            <a class="btn btn-primary" id="BT_Download" target="_blank" href="DownloadDoc?Id=32">
            <i class="icon-file-text icon-large"></i>
            Download Document
            </a>
    

    use this in jQuery

        $('#BT_Download').attr('disabled',true);
    

    add this to css :

        a[disabled="disabled"] {
            pointer-events: none;
        }
    
    0 讨论(0)
提交回复
热议问题