首页 文章

如何仅使用CSS禁用链接?

提问于
浏览
774

有没有办法使用CSS禁用链接?

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

20 回答

  • 0

    CSS只能用于改变样式一些东西 . 你可以用纯CSS做的最好的事情就是隐藏链接 .

    你真正需要的是一些javascript . 以下是使用jQuery库执行所需操作的方法 .

    $('a.current-page').click(function() { return false; });
    
  • 1249
    .disabled {
      pointer-events: none;
      cursor: default;
      opacity: 0.6;
    }
    
    <a href="#" class="disabled">link</a>
    
  • 1

    感谢所有发布解决方案的人,我结合了多种方法来提供更高级的 disabled 功能 . Here is a gist,代码如下 .

    This provides for multiple levels of defense so that Anchors marked as disable actually behave as such.
    Using this approach, you get an anchor that you cannot:
      - click
      - tab to and hit return
      - tabbing to it will move focus to the next focusable element
      - it is aware if the anchor is subsequently enabled
    
    
    1.  Include this css, as it is the first line of defense.  This assumes the selector you use is 'a.disabled'
        a.disabled {
          pointer-events: none;
          cursor: default;
        }
    
     2. Next, instantiate this class such as (with optional selector):
        $ ->
          new AnchorDisabler()
    

    这是coffescript类:

    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 js 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
    
  • 9

    pointer-events属性允许控制HTML元素如何响应鼠标/触摸事件 - 包括CSS悬停/活动状态,Javascript中的单击/点击事件以及光标是否可见 .

    这是 not 唯一的方式 disable a Link ,但是在IE10和所有新浏览器中运行的CSS方式很好:

    .current-page {
      pointer-events: none;
      color: grey;
    }
    
    <a href="#" class="current-page">This link is disabled</a>
    
  • 32

    Demo here
    试试这个

    $('html').on('click', 'a.Link', function(event){
        event.preventDefault();
    });
    
  • 0

    答案已经在问题的评论中 . 为了获得更多可见性,我在这里复制this solution

    .not-active {
      pointer-events: none;
      cursor: default;
      text-decoration: none;
      color: black;
    }
    
    <a href="link.html" class="not-active">Link</a>
    

    有关浏览器支持,请参阅https://caniuse.com/#feat=pointer-events . 如果你需要支持IE,有一个解决方法;见this answer .

    警告:在CSS中使用pointer-events非SVG元素是实验性的 . 该功能曾经是CSS3 UI草案规范的一部分,但由于许多未解决的问题,已被推迟到CSS4 .

  • 124

    只有这样你才能在没有CSS的情况下做到这一点,就是在包装div上设置一个CSS,让你消失,其他东西就是它的位置 .

    例如:

    <div class="disabled">
        <a class="toggleLink" href="wherever">blah</a>
        <span class="toggleLink">blah</span
    </div>
    

    用CSS喜欢

    .disabled a.toggleLink { display: none; }
    span.toggleLink { display: none; }
    .disabled span.toggleLink { display: inline; }
    

    要实际关闭A,您必须更换它的click事件或href,如其他人所述 .

    PS:只是为了澄清我认为这是一个相当凌乱的解决方案,对于搜索引擎优化它也不是最好的,但我相信它是纯粹的CSS最好的 .

  • 28

    如果您想坚持表单上的HTML / CSS,另一个选择是使用按钮 . 设置样式并设置 disabled 属性 .

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

  • 18

    你可以用这个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>
    
  • 10

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

    (我们偶然发现了这一点,因为我们遇到了突然失效的链接问题,因为“响应式”设计导致H2在浏览器窗口移动大小时覆盖它们 . )

  • 2

    pointer-events:none 将禁用链接:

    .disabled {
           pointer-events:none;
    }
    <a href="#" class="disabled">link</a>
    
  • 0
    <a href="#!">1) Link With Non-directed url</a><br><br>
    
    <a href="#!" disabled >2) Link With with disable url</a><br><br>
    
  • 3

    试试这个:

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

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

    .disabled {
      /* Disabled link style */
      color: black;
    }
    
    <a class="disabled" href="javascript:void(0)">LINK</a>
    
  • 3

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

    要移动CSS定义中的逻辑,您必须使用属性选择器 . 这里有些例子 :

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

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

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

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

    此处,将禁用包含路径中的 /keyword/ 的任何链接

    <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属性 . 可能 reltargetdata-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;
    }
    

    浏览器支持

    IE7以来支持属性选择器 . 自IE9起 :not() 选择器 .

  • 7

    我用了:

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

    还不够;在某些浏览器中,它仍显示链接,闪烁 .

    我不得不补充:

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

    CSS无法做到这一点 . CSS仅用于演示 . 你的选择是:

    • 不要在 <a> 标记中包含 href 属性 .

    • 使用JavaScript,查找具有该 class 的锚元素,并相应地删除它们的 hrefonclick 属性 . jQuery会帮助你(NickF展示了如何做类似但更好的事情) .

  • 9
    <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>
    
  • 0

    我通过互联网搜索,发现并不比this好 . 基本上禁用按钮单击功能,只需使用jQuery添加CSS样式,如下所示:

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

    然后再次启用它

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

    检查Firefox和IE 11,它的工作原理 .

  • 120

    可以在CSS中完成它

    .disabled{
      cursor:default;
      pointer-events:none;
      text-decoration:none;
      color:black;
    }
    
    <a href="https://www.google.com" target="_blank" class="disabled">Google</a>
    

    见:

    请注意,不需要 text-decoration: none;color: black; ,但它使链接看起来更像纯文本 .

相关问题