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

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


当前回答

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

其他回答

我在网上搜索了一下,没有比这更好的了。 基本上,要禁用按钮点击功能,只需使用jQuery添加CSS样式,如下所示:

$("#myLink").css({ 'pointer-events': 'none' });

然后再次启用它,这样做

$("#myLink").css({ 'pointer-events': '' });

在Firefox和Internet Explorer 11上进行了检查,可以正常工作。

你也可以试试这个

<style> .btn-disable { pointer-events: none !important; color: currentColor; cursor: not-allowed; opacity: 0.6; text-decoration: none; } </style> <html> <head> <title>NG</title> </head> <style> .btn-disable { pointer-events: none !important; color: currentColor; cursor: not-allowed; opacity: 0.6; text-decoration: none; } </style> <body> <div class="btn-disable"> <input type="button" value="Show"> </div> </body> </html>

从这个解决方案:

(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。

我使用:

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

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

我必须补充一句:

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

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

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