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

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


当前回答

你可以将href属性设置为javascript:void(0):

.disabled { /*禁用链接样式*/ 颜色:黑色; } <a class="disabled" href="javascript:void(0)“链接> < / >

其他回答

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>

如果您希望它只是CSS,禁用逻辑应该由CSS定义。

要移动CSS定义中的逻辑,必须使用属性选择器。下面是一些例子:

禁用具有精确href: =的链接

你可以选择禁用包含特定href值的链接,如下所示:

<a href="//website.com/exact/path">Exact path</a>
[href="//website.com/exact/path"]{
  pointer-events: none;
}

禁用包含路径:*=的链接

在这里,任何包含/keyword/in路径的链接都将被禁用:

<a href="//website.com/keyword/in/path">Contains in path</a>
[href*="/keyword/"]{
  pointer-events: none;
}

禁用以:^=开头的链接

[attribute^=value]操作符的目标是以特定值开头的属性。它允许您丢弃网站和根路径。

<a href="//website.com/begins/with/path">Begins with path</a>
[href^="//website.com/begins/with"]{
  pointer-events: none;
}

你甚至可以用它来禁用非https链接。例如:

a:not([href^="https://"]){
  pointer-events: none;
}

禁用以:$=结尾的链接

[attribute$=value]操作符的目标是一个以特定值结尾的属性。丢弃文件扩展名可能很有用。

<a href="/path/to/file.pdf">Link to pdf</a>
[href$=".pdf"]{
  pointer-events: none;
}

或者其他属性

CSS可以针对任何HTML属性。可以是rel、target、data-custom等等……

<a href="#" target="_blank">Blank link</a>
[target=_blank]{
  pointer-events: none;
}

组合属性选择器

您可以链接多个规则。假设你想要禁用所有的外部链接,但不包括那些指向你网站的链接:

a[href*="//"]:not([href*="my-website.com"]) {
    pointer-events: none;
}

或禁用指向特定网站pdf文件的链接:

<a href="//website.com/path/to/file.jpg">Link to image</a>
[href^="//website.com"][href$=".jpg"] {
  color: red;
}

浏览器支持

从Internet Explorer 7开始就支持属性选择器。从ie9开始使用:not()选择器。

如果你想在表单上只使用HTML/CSS,另一种选择是使用按钮。设置它的样式并设置disabled属性。

如。 http://jsfiddle.net/cFTxH/1/

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

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

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

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