使用单击事件禁用锚点 [英] Disabling an anchor with a click event

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

问题描述

我有一个锚点标记,它有一个链接到它的jQuery点击事件。

I have an anchor tag that has a jQuery click event linked to it.

这个点击事件的第一件事就是通过设置将锚点设置为禁用disabled属性为true。这会激发正确的外观,因为它从我的样式表中选取 myAnchor [disabled] 样式。

The first thing this click event does is to set the anchor as disabled, by setting the disabled attribute to true. This fires the correct look, since it picks up the myAnchor[disabled] style from my stylesheet.

但是,当锚点已禁用设置为true时,它不会停止触发Click事件多次。我想这是因为disabled属性只适用于输入而不是锚点(正如我在尝试之后发现的那样)。

However, it does not stop the Click event from being fired more times while the anchor has disabled set to true. I guess this is because the disabled attribute only works on inputs an not on anchors (as I found out after trying this).

此代码适用于许多内部锚点应用程序,因此向Click事件本身添加一些代码是不可取的,因为我必须将其复制到所有Click方法。即使我使用所需的代码创建一个函数,这意味着我(或者更糟糕的是,其他可能不知道的开发人员)必须记住在所有Click方法中首先放置此行。

This code is general for many anchors inside the application, so adding some code to the Click event itself is not desirable as I will have to copy this to all the Click methods. Even if I create a function with the required code, it means I (or worse, other developers that might not be aware) will have to remember to put this line first in all Click methods.

请记住,锚点也可能在某些时候再次启用,所以我不能只删除点击事件,从那时起启用我需要跟踪其他地方的功能以及点击方法。

Remember that the anchor will probably be enabled at some point again also, so I can't just remove the click event since then when enabling I need to keep track of the function somewhere else as well in addition to the click method.

所以问题是,禁用锚点的正确方法是什么?

So the question is, what is the correct way of disabling an anchor?

推荐答案

您可以执行以下任何操作。

You can do any of the following.

$('a').click(function() { return false; }); 

$('a').off('click');

$("a").unbind("click");

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

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