使用禁用的attr禁用链接 [英] Disable a links using attr disabled

查看:82
本文介绍了使用禁用的attr禁用链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用此功能禁用链接:

I want to know if it's possible to disable links using this :

<a id="link" href=""></a>
$("#link").attr("disabled", false)

这适用于按钮,但不适用于<a>标签.

This works with buttons, but not with <a> tags.

我知道可以这样做:

$('#link').click(function(e) {
    e.preventDefault();
});

或者甚至完全删除href,但是使用我的系统无法正常工作,删除href会导致严重的问题.

Or to even remove the hrefcompletely but using my system this doesn't work, and removing the href will cause serious issues.

所以问题是,是否有一种方法可以禁用标签,即使其不能单击/激活,并像对按钮进行.attr("disabled", false)那样重新激活它?

So the question is, is there a way to just disable the tag, meaning to make it not clickable/active and to re-activate it as we would do .attr("disabled", false) for buttons?

推荐答案

您可以使用CSS设置 pointer-events none:

You can use CSS to set pointer-events to none:

$('#remove').click(function(){
  $('#link').css({'pointer-events': 'auto', 'text-decoration': 'underline'});
});

#link{
  pointer-events: none;
  text-decoration: none;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="link" href="https://stackoverflow.com/">Stackoverflow</a>

<button id="remove">Enable Link</button>

这篇关于使用禁用的attr禁用链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆