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

后端 未结 18 1856
清酒与你
清酒与你 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:11

    The best way is to prevent the default action. In the case of anchor tag, the default behavior is redirecting to href specified address.

    So following javascript works best in the situation:

    $('#ThisLink').click(function(e)
    {
        e.preventDefault();
    });
    
    0 讨论(0)
  • 2020-11-27 03:12

    The cleanest method would be to add a class with pointer-events:none when you want to disable a click. It would function like a normal label.

    .disableClick{
        pointer-events: none;
    }
    
    0 讨论(0)
  • 2020-11-27 03:12

    You could use the onclick event to disable the click action:

    <a href='' id='ThisLink' onclick='return false'>some text</a>
    

    Or you could just use something other than an <a> tag.

    0 讨论(0)
  • 2020-11-27 03:14

    The easyest way

    In your html:

    <a id="foo" disabled="true">xxxxx<a>
    

    In your js:

    $('#foo').attr("disabled", false);
    

    If you use it as attribute works perfectly

    0 讨论(0)
  • 2020-11-27 03:15

    Bootstrap provide us with .disabled class. Please use it.

    But .disabled class only works when the 'a' tag already has class 'btn'. It doesn' t work on any old 'a' tag. The btn class may not be appropriate in some context as it has style connotations. Under the covers, the .disabled class sets pointer-events to none, so you can make CSS to do the same thing as Saroj Aryal and Vitrilo have sugested. (Thank you, Les Nightingill for this advice).

    0 讨论(0)
  • 2020-11-27 03:15

    Create following class in style sheet :

      .ThisLink{
               pointer-events: none;
               cursor: default;
        }
    

    Add this class to you link dynamically as follow.

     <a href='' id='elemID'>some text</a>
    
        //   or using jquery
    <script>
        $('#elemID').addClass('ThisLink');
     </script>
    
    0 讨论(0)
提交回复
热议问题