jQuery disable a link

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

    Here is an alternate css/jQuery solution that I prefer for its terseness and minimized scripting:

    css:

    a.disabled {
      opacity: 0.5;
      pointer-events: none;
      cursor: default;
    }
    

    jQuery:

    $('.disableAfterClick').click(function (e) {
       $(this).addClass('disabled');
    });
    
    0 讨论(0)
  • 2020-11-22 15:31

    This works for links that have the onclick attribute set inline. This also allows you to later remove the "return false" in order to enable it.

            //disable all links matching class
            $('.yourLinkClass').each(function(index) {
                var link = $(this);
                var OnClickValue = link.attr("onclick");
                link.attr("onclick", "return false; " + OnClickValue);
            });
    
            //enable all edit links
            $('.yourLinkClass').each(function (index) {
                var link = $(this);
                var OnClickValue = link.attr("onclick");
                link.attr("onclick", OnClickValue.replace("return false; ", ""));
            });
    
    0 讨论(0)
  • 2020-11-22 15:34

    For others who came here via google like me - here's another approach:

    css:
    .disabled {
      color: grey; // ...whatever
    }
    
    jQuery:
    $('#myLink').click(function (e) {
      e.preventDefault();
      if ($(this).hasClass('disabled'))
        return false; // Do something else in here if required
      else
        window.location.href = $(this).attr('href');
    });
    
    // Elsewhere in your code
    if (disabledCondition == true)
      $('#myLink').addClass('disabled')
    else
      $('#myLink').removeClass('disabled')
    

    Remember: not only this is a css class

    class="buttonstyle"

    but also these two

    class="buttonstyle disabled"

    so you can easily add and remove further classes with jQuery. No need to touch href...

    I love jQuery! ;-)

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

    Try this:

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

    EDIT-

    From your updated code:

     var location= $('#link1').attr("href");
     $("#link1").removeAttr('href');
     $('ul').addClass('expanded');
     $('ul.expanded').fadeIn(300);
     $("#link1").attr("href", location);
    
    0 讨论(0)
  • 2020-11-22 15:36

    you can just hide and show the link as you like

    $(link).hide();
    $(link).show();
    
    0 讨论(0)
  • 2020-11-22 15:37
    $('#myLink').click(function(e) {
        e.preventDefault();
        //do other stuff when a click happens
    });
    

    That will prevent the default behaviour of a hyperlink, which is to visit the specified href.

    From the jQuery tutorial:

    For click and most other events, you can prevent the default behaviour - here, following the link to jquery.com - by calling event.preventDefault() in the event handler

    If you want to preventDefault() only if a certain condition is fulfilled (something is hidden for instance), you could test the visibility of your ul with the class expanded. If it is visible (i.e. not hidden) the link should fire as normal, as the if statement will not be entered, and thus the default behaviour will not be prevented:

    $('ul li').click(function(e) {
        if($('ul.expanded').is(':hidden')) {
            e.preventDefault();
            $('ul').addClass('expanded');
            $('ul.expanded').fadeIn(300);
        } 
    });
    
    0 讨论(0)
提交回复
热议问题