是否有一种方法禁用链接使用CSS?
我有一个名为current-page的类,并希望禁用该类的链接,以便在单击它们时不发生任何操作。
是否有一种方法禁用链接使用CSS?
我有一个名为current-page的类,并希望禁用该类的链接,以便在单击它们时不发生任何操作。
当前回答
您还可以调整另一个元素的大小,使其覆盖链接(使用正确的z-index):这将“吃掉”点击。
(我们偶然发现这一点,因为我们有一个问题,突然不活跃的链接,由于“响应式”设计导致H2覆盖他们时,浏览器窗口的移动大小。)
其他回答
您还可以调整另一个元素的大小,使其覆盖链接(使用正确的z-index):这将“吃掉”点击。
(我们偶然发现这一点,因为我们有一个问题,突然不活跃的链接,由于“响应式”设计导致H2覆盖他们时,浏览器窗口的移动大小。)
我在网上搜索了一下,没有比这更好的了。 基本上,要禁用按钮点击功能,只需使用jQuery添加CSS样式,如下所示:
$("#myLink").css({ 'pointer-events': 'none' });
然后再次启用它,这样做
$("#myLink").css({ 'pointer-events': '' });
在Firefox和Internet Explorer 11上进行了检查,可以正常工作。
你可以用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:澄清一下,我认为这是一个相当不整洁的解决方案,对于SEO来说也不是最好的,但我相信它是纯CSS最好的。
我结合了多种方法来提供一些更高级的禁用功能。这里有一个要点,代码如下。
这提供了多个级别的防御,因此标记为禁用的锚实际上是禁用的。
使用这种方法,你可以得到一个锚,你不能:
点击 按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
你可以使用这个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>