How do you make an anchor link non-clickable or disabled?

后端 未结 18 1886
清酒与你
清酒与你 2020-11-27 02:38

I have an anchor link that I want to disable once the user clicks on it. Or, remove the anchor tag from around the text, but definitely keep the text.



        
相关标签:
18条回答
  • 2020-11-27 03:03

    Try this:

    $('a').contents().unwrap();
    
    0 讨论(0)
  • 2020-11-27 03:04

    I just realized what you were asking for(I hope). Here's an ugly solution

    var preventClick = false;
    
    $('#ThisLink').click(function(e) {
        $(this)
           .css('cursor', 'default')
           .css('text-decoration', 'none')
    
        if (!preventClick) {
            $(this).html($(this).html() + ' lalala');
        }
    
        preventClick = true;
    
        return false;
    });
    
    0 讨论(0)
  • 2020-11-27 03:05

    Jason MacDonald comments worked for me, tested in Chrome, Mozila and IE.

    Added gray color to show disable effect.

    .disable_a_href{
        pointer-events: none;
        **color:#c0c0c0 !important;**
    }
    

    Jquery was selecting only first element in the anchor list, added meta character (*) to select and disable all element with id #ThisLink.

    $("#ThisLink*").addClass("disable_a_href"); 
    
    0 讨论(0)
  • 2020-11-27 03:06

    Write this a single line of jQuery Code

    $('.hyperlink').css('pointer-events','none');
    

    if you want to write in css file

    .hyperlink{
        pointer-events: none;
    }
    
    0 讨论(0)
  • 2020-11-27 03:07
    <a href='javascript:void(0);'>some text</a>
    
    0 讨论(0)
  • 2020-11-27 03:08

    Use pointer-events CSS style. (as Jason MacDonald suggested)

    See MDN https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events. Its supported in most browsers.

    Simple adding "disabled" attribute to anchor will do the job if you have global CSS rule like following:

    a[disabled], a[disabled]:hover {
       pointer-events: none;
       color: #e1e1e1;
    }
    
    0 讨论(0)
提交回复
热议问题