使用 javascript 禁用 a 标签(链接)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1919432/
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
Disabled the a tag (link) by using javascript
提问by Jin Yong
Does anyone how can I disabled the a tag (link) by using javascript?
有没有人如何使用javascript禁用a标签(链接)?
Example:
例子:
<div class="sub-heading">
Contact Details
<a href="./cust_add_edit_customer.php?action=edit_customer_details&cust_code=12761">
<img class="imgVA editIconPad" src="../images/edit0.gif"
alt="Edit Contact Details" border="0" width="20" height="17">
</a>
</div>
I hope to disabled this a tag after a button been clicked.
我希望在单击按钮后禁用此标签。
回答by Annabelle
I think the most user-friendly approach is to hide the link. In your button click handler do:
我认为对用户最友好的方法是隐藏链接。在您的按钮单击处理程序中执行以下操作:
document.getElementById('anchorID').style.visibility = 'hidden';
Then to reenable it:
然后重新启用它:
document.getElementById('anchorID').style.visibility = 'visible';
回答by Eli Grey
Use an onclick="this.onclick=function(){return false}"attribute on the atag. If there's a lot of buttons, you should iterate through them in a JavaScript script that adds an event listener for clickthat is a function that returns false.
onclick="this.onclick=function(){return false}"在a标签上使用属性。如果有很多按钮,您应该在 JavaScript 脚本中遍历它们,该脚本添加一个事件侦听器,因为click它是一个返回 false 的函数。
回答by ACP
Hai
海
function disableAnchor(obj, disable)
{
if (disable) {
var href = obj.getAttribute("href");
if (href && href != "" && href != null) {
obj.setAttribute('href_bak', href);
}
obj.removeAttribute('href');
obj.style.color="gray";
}
else {
obj.setAttribute('href', obj.attributes['href_bak'].nodeValue);
obj.style.color="blue";
}
}
or
或者
var Link_Enabled_Flag = false; // disable links - background process changes this to true when it's done
function Check_Link_Enabled(){ return Link_Enabled_Flag; }
<a href="wherever.com" onclick="return Check_Link_Enabled()"></a>
or
或者
IE and Firefox compatible javascript to enable or disable an anchor tag
IE 和 Firefox 兼容 javascript 以启用或禁用锚标记
onclick="disableAnchor(this,'verify')"
function disableAnchor(Check_Obj, Check_Id){
var Anchor_id = 's';
thisCheckbox = document.getElementById(Check_Id);
thisAnchor = document.getElementById(Anchor_id);
if(thisCheckbox.checked){
//alert('Y1');
Check_Obj.setAttribute('href',''); //Check_Obj.attributes['href_bak'].nodeValue
Check_Obj.style.color="blue";
//alert('Y2');
}
else{
//alert('N1');
var href = Check_Obj.getAttribute('href');
//alert(href);
if(href && href != "" && href != null){
Check_Obj.setAttribute('href_bak', href);
}
Check_Obj.removeAttribute('href');
Check_Obj.style.color="gray";
//alert('N2');
}
}
回答by nornagon
Add an idattribute to the atag you want to disable, then:
id向a要禁用的标签添加一个属性,然后:
document.getElementById('the_id').href = '#';
回答by Luke101
You can use jquery
你可以使用 jquery
$('sub-heading').attr("disabled", "true");
回答by Per G
Remove the attribute href like:
删除属性 href 如下:
<a>Edit</a>
回答by eung
Also you can do this way...
你也可以这样做...
<a style="cursor:Default; text-decoration:none; color:inherit;">.....</a>
回答by cregox
You can do it like this:
你可以这样做:
<a href="javascript:if (links_enabled) document.location='www.example.com';">enabled or disabled link</a>
<br/>
<a href="javascript:links_enabled = true;">enable link</a>
<br/>
<a href="javascript:links_enabled = !links_enabled;">toggle link</a>
I find this very elegant, and it will also make links work only javascript is enabled. In other words, links are disabled by default.
我觉得这非常优雅,而且它也会使链接在启用 javascript 的情况下工作。换句话说,默认情况下禁用链接。

