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

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


当前回答

CSS只能用来改变一些东西的样式。使用纯CSS所能做的最好的事情就是完全隐藏链接。

您真正需要的是一些JavaScript代码。下面是如何使用jQuery库完成您想做的事情。

$('a.current-page').click(function() { return false; });

其他回答

.disabled { pointer-events:没有; 光标:违约; 透明度:0.6; } <a href="#" class="disabled">link</a>

试试这个:

<style>
    .btn-disable {
        display: inline-block;
        pointer-events: none;
    }
</style>

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

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

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

点击 按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

您还可以调整另一个元素的大小,使其覆盖链接(使用正确的z-index):这将“吃掉”点击。

(我们偶然发现这一点,因为我们有一个问题,突然不活跃的链接,由于“响应式”设计导致H2覆盖他们时,浏览器窗口的移动大小。)

我使用:

.current-page a:hover {
    pointer-events: none !important;
}

这还不够;在某些浏览器中,它仍然会闪烁着显示链接。

我必须补充一句:

.current-page a {
    cursor: text !important;
}