How to disable HTML links

后端 未结 14 1718
刺人心
刺人心 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: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
    

提交回复
热议问题