How to disable HTML links

后端 未结 14 1365
刺人心
刺人心 2020-11-22 13:00

I have a link button inside a which I have to disable. This works on IE but not working in Firefox and Chrome. Structure is - Link inside a <

相关标签:
14条回答
  • 2020-11-22 13:27

    Bootstrap 4.1 provides a class named disabled and aria-disabled="true" attribute.

    example"

    <a href="#" 
            class="btn btn-primary btn-lg disabled" 
            tabindex="-1" 
            role="button" aria-disabled="true"
    >
        Primary link
    </a>
    

    More is on getbootstrap.com

    So if you want to make it dynamically, and you don't want to care if it is button or ancor than in JS script you need something like that

       let $btn=$('.myClass');
       $btn.attr('disabled', true);
       if ($btn[0].tagName == 'A'){
            $btn.off();
            $btn.addClass('disabled');
            $btn.attr('aria-disabled', true);
       }
    

    But be carefull

    The solution only works on links with classes btn btn-link.

    Sometimes bootstrap recommends using card-link class, in this case solution will not work.

    0 讨论(0)
  • 2020-11-22 13:28

    Thanks to everyone that posted solutions (especially @AdrianoRepetti), I combined multiple approaches to provide some more advanced disabled functionality (and it works cross browser). The code is below (both ES2015 and coffeescript based on your preference).

    This provides for multiple levels of defense so that Anchors marked as disable actually behave as such. Using this approach, you get an anchor that you cannot:

    • click
    • tab to and hit return
    • tabbing to it will move focus to the next focusable element
    • it is aware if the anchor is subsequently enabled

    How to

    1. Include this css, as it is the first line of defense. This assumes the selector you use is a.disabled

      a.disabled {
        pointer-events: none;
        cursor: default;
      }
      
    2. Next, instantiate this class on ready (with optional selector):

        new AnchorDisabler()
      

    ES2015 Class

    npm install -S key.js

    import {Key, Keycodes} from 'key.js'
    
    export default class AnchorDisabler {
      constructor (config = { selector: 'a.disabled' }) {
        this.config = config
        $(this.config.selector)
          .click((ev) => this.onClick(ev))
          .keyup((ev) => this.onKeyup(ev))
          .focus((ev) => this.onFocus(ev))
      }
    
      isStillDisabled (ev) {
        //  since disabled can be a class or an attribute, and it can be dynamically removed, always recheck on a watched event
        let target = $(ev.target)
        if (target.hasClass('disabled') || target.prop('disabled') == 'disabled') {
          return true
        }
        else {
          return false
        }
      }
    
      onFocus (ev) {
        //  if an attempt is made to focus on a disabled element, just move it along to the next focusable one.
        if (!this.isStillDisabled(ev)) {
          return
        }
    
        let focusables = $(':focusable')
        if (!focusables) {
          return
        }
    
        let current = focusables.index(ev.target)
        let next = null
        if (focusables.eq(current + 1).length) {
          next = focusables.eq(current + 1)
        } else {
          next = focusables.eq(0)
        }
    
        if (next) {
          next.focus()
        }
      }
    
      onClick (ev) {
        // disabled could be dynamically removed
        if (!this.isStillDisabled(ev)) {
          return
        }
    
        ev.preventDefault()
        return false
      }
    
      onKeyup (ev) {
        // We are only interested in disabling Enter so get out fast
        if (Key.isNot(ev, Keycodes.ENTER)) {
          return
        }
    
        // disabled could be dynamically removed
        if (!this.isStillDisabled(ev)) {
          return
        }
    
        ev.preventDefault()
        return false
      }
    }
    

    Coffescript class:

    class AnchorDisabler
      constructor: (selector = 'a.disabled') ->
        $(selector).click(@onClick).keyup(@onKeyup).focus(@onFocus)
    
      isStillDisabled: (ev) =>
        ### since disabled can be a class or an attribute, and it can be dynamically removed, always recheck on a watched event ###
        target = $(ev.target)
        return true if target.hasClass('disabled')
        return true if target.attr('disabled') is 'disabled'
        return false
    
      onFocus: (ev) =>
        ### if an attempt is made to focus on a disabled element, just move it along to the next focusable one. ###
        return unless @isStillDisabled(ev)
    
        focusables = $(':focusable')
        return unless focusables
    
        current = focusables.index(ev.target)
        next = (if focusables.eq(current + 1).length then focusables.eq(current + 1) else focusables.eq(0))
    
        next.focus() if next
    
    
      onClick: (ev) =>
        # disabled could be dynamically removed
        return unless @isStillDisabled(ev)
    
        ev.preventDefault()
        return false
    
      onKeyup: (ev) =>
    
        # 13 is the js key code for Enter, we are only interested in disabling that so get out fast
        code = ev.keyCode or ev.which
        return unless code is 13
    
        # disabled could be dynamically removed
        return unless @isStillDisabled(ev)
    
        ev.preventDefault()
        return false
    
    0 讨论(0)
  • 2020-11-22 13:31

    I think a lot of these are over thinking. Add a class of whatever you want, like disabled_link.
    Then make the css have .disabled_link { display: none }
    Boom now the user can't see the link so you won't have to worry about them clicking it. If they do something to satisfy the link being clickable, simply remove the class with jQuery:
    $("a.disabled_link").removeClass("super_disabled"). Boom done!

    0 讨论(0)
  • 2020-11-22 13:37

    You can use this to disabled the Hyperlink of asp.net or link buttons in html.

    $("td > a").attr("disabled", "disabled").on("click", function() {
        return false; 
    });
    
    0 讨论(0)
  • 2020-11-22 13:39

    I've ended up with the solution below, which can work with either an attribute, <a href="..." disabled="disabled">, or a class <a href="..." class="disabled">:

    CSS Styles:

    a[disabled=disabled], a.disabled {
        color: gray;
        cursor: default;
    }
    
    a[disabled=disabled]:hover, a.disabled:hover {
        text-decoration: none;
    }
    

    Javascript (in jQuery ready):

    $("a[disabled], a.disabled").on("click", function(e){
    
        var $this = $(this);
        if ($this.is("[disabled=disabled]") || $this.hasClass("disabled"))
            e.preventDefault();
    })
    
    0 讨论(0)
  • 2020-11-22 13:39

    There is one other possible way, and the one that I like best. Basically it's the same way lightbox disables a whole page, by placing a div and fiddling with z-index. Here is relevant snippets from a project of mine. This works in all browsers!!!!!

    Javascript (jQuery):

    var windowResizer = function(){
            var offset = $('#back').offset();   
            var buttontop = offset.top;
            var buttonleft = offset.left;
            $('#backdisabler').css({'top':buttontop,'left':buttonleft,'visibility':'visible'});
            offset = $('#next').offset();
            buttontop = offset.top;
            buttonleft = offset.left;
            $('#nextdisabler').css({'top':buttontop,'left':buttonleft,'visibility':'visible'});
    }
    
    $(document).ready(function() {
        $(window).resize(function() {   
            setTimeout(function() {
                windowResizer();
            }, 5); //when the maximize/restore buttons are pressed, we have to wait or it will fire to fast
        });
    });
    

    and in html

    <a href="" id="back" style="float: left"><img src="images/icons/back.png" style="height: 50px; width: 50px" /></a>
    <a href="" id="next" style="float: right"><img src="images/icons/next.png" style="height: 50px; width: 50px" /></a>
    <img id="backdisabler" src="images/icons/disabled.png" style="visibility: hidden; position: absolute; padding: 5px; height: 62px; width: 62px; z-index: 9000"/>
    <img id="nextdisabler" src="images/icons/disabled.png" style="visibility: hidden; position: absolute; padding: 5px; height: 62px; width: 62px; z-index: 9000"/>
    

    So the resizer finds the anchor's (the images are just arrows) locations and places the disabler on top. The disabler's image is a translucent grey square (change the width/height of the disablers in the html to match your link) to show that it is disabled. The floating allows the page to resize dynamically, and the disablers will follow suit in windowResizer(). You can find suitable images through google. I have placed the relevant css inline for simplicity.

    then based on some condition,

    $('#backdisabler').css({'visibility':'hidden'});
    $('#nextdisabler').css({'visibility':'visible'});
    
    0 讨论(0)
提交回复
热议问题