disable a hyperlink using jQuery

后端 未结 11 1623
悲&欢浪女
悲&欢浪女 2020-11-29 02:06
Click me

I did

$(\'.my-link\').attr(\'disabled\', true);
相关标签:
11条回答
  • 2020-11-29 02:38
    $('.my-link').click(function(e) { e.preventDefault(); }); 
    

    You could use:

    $('.my-link').click(function(e) { return false; }); 
    

    But I don't like to use this myself as it is more cryptic, even though it is used extensively throughout much jQuery code.

    0 讨论(0)
  • 2020-11-29 02:39

    The disabled attribute isn't valid on all HTML elements I believe, see the MSDN article. That and the proper value for disabled is simply "disabled". Your best approach is to bind a click function that returns false.

    0 讨论(0)
  • 2020-11-29 02:43

    You can bind a click handler that returns false:

    $('.my-link').click(function () {return false;});
    

    To re-enable it again, unbind the handler:

    $('.my-link').unbind('click');
    

    Note that disabled doesn't work because it is designed for form inputs only.


    jQuery has anticipated this already, providing a shortcut as of jQuery 1.4.3:

    $('.my-link').bind('click', false);
    

    And to unbind / re-enable:

    $('.my-link').unbind('click', false);
    
    0 讨论(0)
  • 2020-11-29 02:43

    I know it's an old question but it seems unsolved still. Follows my solution...

    Simply add this global handler:

    $('a').click(function()
    {
         return ($(this).attr('disabled')) ? false : true;
    });
    

    Here's a quick demo: http://jsbin.com/akihik/3

    you can even add a bit of css to give a different style to all the links with the disabled attribute.

    e.g

    a[disabled]
    {
        color: grey; 
    }
    

    Anyway it seems that the disabled attribute is not valid for a tags. If you prefer to follow the w3c specs you can easily adopt an html5 compliant data-disabled attribute. In this case you have to modify the previous snippet and use $(this).data('disabled').

    0 讨论(0)
  • 2020-11-29 02:49

    Below will replace the link with it's text

    $('a').each(function () {
        $(this).replaceWith($(this).text());
    });
    

    Edit :

    Above given code will work with hyperlinks with text only, it will not work with images. When we'll try it with image link it won't show any image.

    To make this code compatible with image links following will work fine

    // below given function will replace links with images i.e. for image links
    $('a img').each(function () {
        var image = this.src;
        var img = $('<img>', { src: image });
        $(this).parent().replaceWith(img);
    });
    
    // This piece of code will replace links with its text i.e. for text links
    $('a').each(function () {
        $(this).replaceWith($(this).text());
    });
    

    explanation : In above given code snippets, in first snippet we are replacing all the image links with it's images only. After that we are replacing text links with it's text.

    0 讨论(0)
  • 2020-11-29 02:51
    function EnableHyperLink(id) {
            $('#' + id).attr('onclick', 'Pagination("' + id + '")');//onclick event which u 
            $('#' + id).addClass('enable-link');
            $('#' + id).removeClass('disable-link');
        }
    
        function DisableHyperLink(id) {
            $('#' + id).addClass('disable-link');
            $('#' + id).removeClass('enable-link');
            $('#' + id).removeAttr('onclick');
        }
    
    .disable-link
    {
        text-decoration: none !important;
        color: black !important;
        cursor: default;
    }
    .enable-link
    {
        text-decoration: underline !important;
        color: #075798 !important;
        cursor: pointer !important;
    }
    
    0 讨论(0)
提交回复
热议问题