使用javascript禁用a标记(链接)
有人知道如何使用javascript禁用a标记(链接)吗 例如:使用javascript禁用a标记(链接),javascript,Javascript,有人知道如何使用javascript禁用a标记(链接)吗 例如: <div class="sub-heading"> Contact Details <a href="./cust_add_edit_customer.php?action=edit_customer_details&cust_code=12761"> <img class="imgVA editI
<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>
联系方式
我希望在单击按钮后禁用此标记。在a
标记上使用onclick=“this.onclick=function(){return false}”
属性。如果有很多按钮,您应该在JavaScript脚本中对它们进行迭代,该脚本为单击添加一个事件侦听器,该函数返回false。
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";
}
}
或
将id
属性添加到要禁用的a
标记,然后:
document.getElementById('the_id').href = '#';
您可以使用jquery
$('sub-heading').attr("disabled", "true");
我认为最方便用户的方法是隐藏链接。在按钮中,单击处理程序执行以下操作:
document.getElementById('anchorID').style.visibility = 'hidden';
然后重新启用它:
document.getElementById('anchorID').style.visibility = 'visible';
您可以这样做:
<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>
我觉得这非常优雅,而且只有启用了javascript,链接才能正常工作。换句话说,默认情况下禁用链接。删除属性href,如:
<a>Edit</a>
编辑
禁用属性不会停止单击标签。但是我设计了一个标签,标签上有一个按钮,并且至少添加了“disabled”:按钮的样式是真的,它有一个真正的disabled按钮
现在,如果您想停止单击,只需使用css属性“指针事件:无”
所以你可以用这样的方法:
$('.sub-heading a').attr("disabled", "true");
$('.sub-heading a').css('pointer-events', 'none');
它会成功的;)
如果您想通过单击其他按钮来执行此操作:
jQuery('.my-other-button').on('click', function() {
$('.sub-heading a').attr("disabled", "true");
$('.sub-heading a').css('pointer-events', 'none');
});
你想禁用或隐藏它你说的“禁用”是什么意思?我假设您不希望用户能够单击它并导航到其目标。您是否还需要阻止右键单击“在新窗口/选项卡中打开”?可能的重复项仍然可以单击,并将页面滚动到顶部。选择器不正确。对于示例HTML,它应该是$('.sub-heading a')。attr(“disabled”,“true”)
此外,disabled不是
元素的属性。
$('.sub-heading a').attr("disabled", "true");
$('.sub-heading a').css('pointer-events', 'none');
jQuery('.my-other-button').on('click', function() {
$('.sub-heading a').attr("disabled", "true");
$('.sub-heading a').css('pointer-events', 'none');
});