jQuery disable a link

前端 未结 17 1194
半阙折子戏
半阙折子戏 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:21

    You should find you answer here.

    Thanks @Will and @Matt for this elegant solution.

    jQuery('#path .to .your a').each(function(){
        var $t = jQuery(this);
        $t.after($t.text());
        $t.remove();
    });
    
    0 讨论(0)
  • 2020-11-22 15:23

    Just use $("a").prop("disabled", true);. This will really disable de link element. Needs to be prop("disabled", true). Don't use attr("disabled", true)

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

    Just trigger stuff, set some flag, return false. If flag is set - do nothing.

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

    Just set preventDefault and return false

       $('#your-identifier').click(function(e) {
            e.preventDefault();
            return false;
        });
    

    This will be disabled link but still, you will see a clickable icon(hand) icon. You can remove that too with below

    $('#your-identifier').css('cursor', 'auto');
    
    0 讨论(0)
  • 2020-11-22 15:25

    unbind() was deprecated in jQuery 3, use the off() method instead:

    $("a").off("click");
    
    0 讨论(0)
  • 2020-11-22 15:28

    My fav in "checkout to edit an item and prevent -wild wild west clicks to anywhere- while in a checkout" functions

    $('a').click(function(e) {
        var = $(this).attr('disabled');
        if (var === 'disabled') {
            e.preventDefault();
        }
    });
    

    So if i want that all external links in a second action toolbar should be disabled while in the "edit-mode" as described above, i'll add in the edit function

    $('#actionToolbar .external').attr('disabled', true);
    

    Link example after fire:

    <a href="http://goo.gl" target="elsewhere" class="external" disabled="disabled">Google</a>
    

    And now you CAN use disabled property for links

    Cheers!

    0 讨论(0)
提交回复
热议问题