根据条件,我需要禁用锚点链接,如果某些数据填入该字段,则应将其作为超链接工作,否则该链接不应存在。对此有任何想法都欢迎。
根据条件,我需要禁用锚点链接,如果某些数据填入该字段,则应将其作为超链接工作,否则该链接不应存在。对此有任何想法都欢迎。
案例1:
禁用:
document.getElementById(id).style.pointerEvents="none";
document.getElementById(id).style.cursor="default";
启用:
document.getElementById(id).style.pointerEvents="auto";
document.getElementById(id).style.cursor="pointer";
情况2:
如果您希望链接消失(而不仅是禁用它):
document.getElementById(id).style.display="none";
要将其找回:
document.getElementById(id).style.display="block"; //change block to what you want.
第三种情况:
如果您想隐藏它并保留空间:
document.getElementById(id).style.visibility="hidden";
要重新获得它:
document.getElementById(id).style.visibility="visible";
我无法理解你的问题正文,所以我将回答你问题的标题...
如何使用JavaScript禁用锚点?
if (condition) {
document.getElementsByTagName('a')[0].removeAttribute('href');
}
因为所有人都在使用它,对吧?
if (condition) {
$('a').first().removeAttr('href');
}
removeAtt
也可能会失败 :) - thirtydotr
:) - alexif(!data){
$('#linkID').click(function(e) {
e.preventDefault();
});
}
使用Prototype
if(!data){
$('linkID').observe('click', function(event) { event.stop() });
}
<a href="javascript:check()">my link</a>
function check(){
var data =document.getElementById("yourdatafield").value;
if(data)
window.location="your_link_location";
}
$('#anchor_tag').attr("disabled", true);
启用:
$('#anchor_tag').attr("disabled", false);