有没有一种使用CSS禁用链接的方法?
我有一个名为 current-page
的类,并希望拥有此类的链接被禁用,以便在单击时不执行任何操作。
试试这个:
<style>
.btn-disable {
display: inline-block;
pointer-events: none;
}
</style>
pointer-events: none
只能阻止使用指针设备点击链接。但是,通过按下 Tab 键直到链接获得焦点,然后再按下空格键仍然可以“点击”链接。 - undefinedpointer-events 属性允许控制 HTML 元素对鼠标/触摸事件的响应方式,包括 CSS hover/active 状态、JavaScript 中的 click/tap 事件以及光标是否可见。
这并不是唯一禁用链接的方法,但它是一种在 Internet Explorer 10(及更高版本)和所有新浏览器中有效的好的 CSS 方法:
.current-page {
pointer-events: none;
color: grey;
}
<a href="#" class="current-page">This link is disabled</a>
pointer-events: none
只能防止指针设备点击链接。通过按Tab键直到链接获得焦点,然后按下空格仍然会“点击”链接。 - undefined$("#myLink").css({ 'pointer-events': 'none' });
$("#myLink").css({ 'pointer-events': '' });
这段代码在Firefox和Internet Explorer 11上经过检查,能够正常工作。
pointer-events: none
只能阻止指针设备点击链接。但是,通过按Tab键直到链接获得焦点,然后按下空格仍然可以“点击”链接。 - undefineda.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>
body{
font-family: 'Roboto', sans-serif;
font-weight: 500;
}
a.disable{
pointer-events: none;
color: black;
text-decoration: none;
}
<a href="https://example.com">Normal</a>
<a href="https://example.com" class="disable">Disable</a>
pointer-events: none
只能阻止指针设备点击链接。通过按 Tab 键直到链接获得焦点,然后按下空格仍然会“点击”链接。 - undefineddisabled
功能。 这里是一个代码片段,下面是代码。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
JS
或其他任何内容无关,而是关于CSS
! - Mehdi Dehghani这里演示
试试这个
$('html').on('click', 'a.Link', function(event){
event.preventDefault();
});
JS
或其他任何东西无关,而是关于CSS
! - Mehdi Dehghanidocument.querySelectorAll('a').forEach(button => {
button.addEventListener('click', (e) => console.log(e.target.textContent));
});
a {
pointer-events: none;
}
<li><a href="#a">link A</a>
<li><a href="#b">link B</a>
<li><a href="#c">link C</a>
2个不完美的解决方案
你可以添加 tabIndex="-1"
,这样你就无法通过Tab键访问链接。
当然,如果你预先添加了这个属性,那么你就禁用了键盘用户的链接。如果你在运行时添加它,那么你并没有通过CSS禁用链接。
为每个链接创建一个伪链接元素,通过CSS隐藏其中一个或另一个。
document.querySelectorAll('a').forEach(button => {
button.addEventListener('click', (e) => console.log(e.target.textContent));
});
const container = document.querySelector('#container');
document.querySelector('button').addEventListener('click', () => {
container.classList.toggle('disable-links');
});
.disabled-link {
text-decoration: underline;
opacity: 25%;
}
.links .disabled-link {
display: none;
}
.disable-links .disabled-link {
display: initial;
}
.disable-links a {
display: none;
}
<div id="container" class="links">
<li><a href="#a">link A</a><span class="disabled-link">link A</span>
<li><a href="#b">link B</a><span class="disabled-link">link B</span>
<li><a href="#c">link C</a><span class="disabled-link">link C</span>
</div>
<button type="button">enable/disable links</button>
hidden
属性来禁用。然后可以使用 CSS 选择器例如a[hidden]
来选择锚点并相应地进行样式设置。 - Armen Michaelidisplay: block
或其他值,你可以指示它们这样做。但是hidden
并不总是适用的——它适用于无关紧要的元素,从问题中不清楚为什么链接应该被禁用。这可能是XY问题的一个例子。 - Armen Michaelipointer-events: none
只是阻止了指针设备点击链接。通过按下 Tab 键直到链接获得焦点,然后按下空格仍然可以“点击”链接。 - undefined