如何在 jquery 中禁用锚标记?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23385741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 02:16:05  来源:igfitidea点击:

how to disable anchor tag in jquery?

jquerybuttonanchor

提问by Dhivya

Here,How to disable this link by using button tab?Is it possible?

在这里,如何使用按钮选项卡禁用此链接?这可能吗?

 <a href="http://www.google.com">
    <button>click here for go to yahoo</button>
</a>

回答by user3519216

Give ids and disable at the run-time.

在运行时提供 id 和禁用。

HTML:

HTML:

<a id="disableanchor" href="http://www.google.com">
        <button id="buttononclickdisable">click here for go to yahoo</button>
</a>

Javascript:

Javascript:

$('#buttononclickdisable').click(function(){
     $('#disableanchor').attr("disabled","disabled");
});

Or remove the 'click' event listener.

或者删除“单击”事件侦听器。

$("#anchorid").off('click');

回答by Grant Thomas

To essentially disable it, you could likely do:

要基本上禁用它,您可能会执行以下操作:

$("a[href='http://www.google.com']").click(function(e) {
  e.preventDefault();
});

But it would be clearer and more precise if you added a class or id and targeted that in the selector. And what this really does is prevent the action from taking place, it doesn't handle management of state, hence making it obvious that the link is not going to work when clicked.

但是如果你添加一个类或 id 并在选择器中定位它,它会更清晰、更精确。这样做的真正作用是阻止操作发生,它不处理state 的管理,因此很明显链接在单击时将不起作用。

回答by Juhil Somaiya

You can something like as follow:

你可以像下面这样:

Remove the 'href' attribute from the anchor tag ($target will be your anchor tag):

从锚标记中删除 'href' 属性($target 将是您的锚标记):

$target.removeAttr('href');

回答by manoj reddy

 $('#disableanchor').attr("disabled","disabled");// is not working with anchor tag

$("#anchorid").off('click'); //this is working

回答by Gillu13

Surprisingly it is not such a straightforward task since anchor tags do not have a disabled attribute (on the contrary of input tags, select tags, textarea tags, etc.).

令人惊讶的是,这并不是一项简单的任务,因为锚标签没有禁用属性(与输入标签、选择标签、文本区域标签等相反)。

The workaround I often use is first to define a class with pointer-events set to none (CSS):

我经常使用的解决方法是首先定义一个指针事件设置为无(CSS)的类:

.disable-click{
    pointer-events:none;
}

Then I use Jquery (1.0+) to disable/enable the "clickability" of the anchor tag in question by adding/removing the class previously defined:

然后我使用 Jquery (1.0+) 通过添加/删除先前定义的类来禁用/启用有问题的锚标记的“可点击性”:

$("#my-a-tag-id").addClass("disable-click");
$("#my-a-tag-id").removeClass("disable-click");