我有一个链接按钮内<td>,我必须禁用。这适用于IE,但不适用于Firefox和Chrome。

我尝试了以下所有方法,但在Firefox(使用1.4.2 js)上都无法工作:

$(".myLink").attr('disabled', 'disabled');

$(".myLink").attr('disabled', true);

$(".myLink").attr('disabled', 'true');

注意-我不能注销锚标记的点击功能,因为它是动态注册的。我必须在禁用模式下显示链接。


当前回答

禁用在触摸设备上访问另一个页面的链接:

if (control == false)
  document.getElementById('id_link').setAttribute('href', '#');
else
  document.getElementById('id_link').setAttribute('href', 'page/link.html');
end if;

其他回答

感谢每个发布解决方案的人(特别是@AdrianoRepetti),我结合了多种方法来提供一些更高级的禁用功能(它可以跨浏览器工作)。代码如下(ES2015和coffeescript根据您的喜好)。

这提供了多个级别的防御,因此标记为禁用的锚实际上是这样的行为。 使用这种方法,你可以得到一个锚,你不能:

点击 按TAB键返回 标签将焦点移动到下一个可聚焦的元素 它知道锚是否随后被启用


如何

包括这个css,因为它是第一道防线。这假设你使用的选择器是a.disabled a.disabled { pointer-events:没有; 光标:违约; } 接下来,在ready上实例化这个类(使用可选的选择器): 新的AnchorDisabler ()


ES2015类

安装-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
  }
}

咖啡类:

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

我认为很多人都想多了。添加一个你想要的类,比如disabled_link。然后使css具有.disabled_link {display: none} 现在用户不能看到链接,所以你不必担心他们点击它。如果他们做了一些事情来满足链接是可点击的,只需删除类jQuery: $("a.disabled_link"). removeclass ("super_disabled")。做得繁荣!

还有另一种可能的办法,也是我最喜欢的办法。基本上,这与lightbox禁用整个页面的方式是一样的,通过放置一个div和摆弄z-index。以下是我的一个项目的相关片段。这适用于所有浏览器!!!!!

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
    });
});

在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"/>

因此,调整器找到锚的位置(图像只是箭头),并将禁用器放在顶部。禁用器的图像是一个半透明的灰色正方形(改变html中禁用器的宽度/高度以匹配您的链接),以显示它是禁用的。浮动允许页面动态地调整大小,并且在windowResizer()中禁用程序也会这样做。你可以通过谷歌找到合适的图片。为了简单起见,我将相关的css内联放置。

然后基于某些条件,

$('#backdisabler').css({'visibility':'hidden'});
$('#nextdisabler').css({'visibility':'visible'});

Bootstrap 4.1提供了一个名为disabled的类和aria-disabled="true"属性。

例子”

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

更多内容请访问getbootstrap.com

如果你想动态地创建它,你不想关心它是button还是ancor than 在JS脚本中你需要这样的东西

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

但是要小心

该解决方案仅适用于具有btn btn-link类的链接。

有时bootstrap建议使用card-link类,在这种情况下解决方案将不起作用。

你不能禁用一个链接,如果你想点击事件不应该触发,然后简单地删除该链接的动作。

$(td).find('a').attr('href', '');

有关更多信息:-元素可以被禁用