是否有一种方法禁用链接使用CSS?

我有一个名为current-page的类,并希望禁用该类的链接,以便在单击它们时不发生任何操作。


当前回答

你可以使用这个CSS内容:

a.button,button { display: inline-block; padding: 6px 15px; margin: 5px; line-height: 1.42857143; text-align: center; white-space: nowrap; vertical-align: middle; -ms-touch-action: manipulation; touch-action: manipulation; cursor: pointer; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; background-image: none; border: 1px solid rgba(0, 0, 0, 0); border-radius: 4px; -moz-box-shadow: inset 0 3px 20px 0 #cdcdcd; -webkit-box-shadow: inset 0 3px 20px 0 #cdcdcd; box-shadow: inset 0 3px 20px 0 #cdcdcd; } a[disabled].button,button[disabled] { cursor: not-allowed; opacity: 0.4; pointer-events: none; -webkit-touch-callout: none; } a.button:active:not([disabled]),button:active:not([disabled]) { background-color: transparent !important; color: #2a2a2a !important; outline: 0; -webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, .5); box-shadow: inset 0 3px 5px rgba(0, 0, 0, .5); } <button disabled="disabled">disabled!</button> <button>click me!</button> <a href="http://royansoft.com" disabled="disabled" class="button">test</a> <a href="http://royansoft.com" class="button">test2</a>

其他回答

指针事件属性允许控制HTML元素如何 响应鼠标/触摸事件-包括CSS悬停/活动状态, 在JavaScript中点击/点击事件,以及光标是否在 可见。

这不是禁用链接的唯一方法,但这是一个很好的CSS方法,适用于Internet Explorer 10(及更高版本)和所有新浏览器:

.current-page { pointer-events:没有; 颜色:灰色; } <a href="#" class="current-page">该链接被禁用</a> .

Bootstrap禁用链接

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

<a href="#" class="btn btn-default btn-lg disabled" role="button">Link</a>

Bootstrap禁用按钮,但它看起来像链接

<button type="button" class="btn btn-link">Link</button>

我结合了多种方法来提供一些更高级的禁用功能。这里有一个要点,代码如下。

这提供了多个级别的防御,因此标记为禁用的锚实际上是禁用的。

使用这种方法,你可以得到一个锚,你不能:

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

 

Include this CSS content, as it is the first line of defense. This assumes the selector you use is 'a.disabled'. a.disabled { pointer-events: none; cursor: default; } Next, instantiate this class such as (with optional selector): $ -> new AnchorDisabler() Here is the CoffeeScript 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 JavaScript 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

从这个解决方案:

(aria-current =“页面”){ pointer-events:没有; 光标:违约; 文字修饰:没有; 颜色:黑色; } <a href=" Link .html" aria-current="page">Link</a> .html

有关浏览器支持,请参见https://caniuse.com/#feat=pointer-events。如果你需要支持ie浏览器,有一个变通办法;请看这个答案。

警告:在CSS中对非svg元素使用指针事件是实验性的。该特性曾经是css3 UI草案规范的一部分,但由于许多悬而未决的问题,已被推迟到css4。

在CSS中可以做到这一点:

.disabled { 光标:违约; pointer-events:没有; 文字修饰:没有; 颜色:黑色; } <a href="https://www.google.com" target="_blank" class="disabled">谷歌</a>

看:

请注意文字装饰:无;颜色:黑色;不是必需的,但它使链接看起来更像纯文本。