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

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


当前回答

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

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

其他回答

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

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

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

如果您希望它只是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()选择器。

<a href="#!">1)链接非定向url</a><br><br> <a href="#!" disabled >2) Link With With disable url</a><br><br>

身体{ font-family: 'Roboto', sans-serif; 粗细:500; } a.disable { pointer-events:没有; 颜色:黑色; 文字修饰:没有; } 正常< a href = " https://example.com " > < / > <a href="https://example.com" class="disable"> disable </a> .

在CSS中可以做到这一点:

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

看:

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