我在 php 页面的 <div>
标签内有 5(说)个链接。当访问者单击任何一个链接时,必须禁用所有5个链接。如何使这成为可能?我有一些想法可以禁用用户单击的链接
<a href="someaction" onClick='this.disabled=true;'>LINK 1</a>
<a href="someaction" onClick='this.disabled=true;'>LINK 2</a>
<a href="someaction" onClick='this.disabled=true;'>LINK 3</a>
<a href="someaction" onClick='this.disabled=true;'>LINK 4</a>
<a href="someaction" onClick='this.disabled=true;'>LINK 5</a>
因此,如果我单击链接 1,则只有链接 1 将被禁用。我需要禁用所有 5 个链接(只有这五个链接而不是页面中的其他链接)。
建议我简单而最好的:)方法
<a>
标签不支持disabled
属性,只有input
、textarea
、button
支持
如果要使链接不可点击,则可以将属性href
为空:
<a href="someaction" onClick='this.href="";'>LINK 5</a>
或者你可以模拟disabled
属性:
<a href="someaction" onClick='if(this.disabled){ return false; } else { this.disabled = true; }'>LINK 5</a>
它将防止将来的点击。
不确定这是否是最好的方法,但可以肯定的是,它会完成这项工作。
function removeLink(id){
document.getElementById(id).innerHTML = 'LINK 1<br />LINK 2<br />LINK 3<br />LINK 4<br />LINK 5<br />';
}
<span id="removeLinkId">
<a href="someaction" onClick="removeLinks('removeLinkId');">LINK 1</a>
<a href="someaction" onClick="removeLinks('removeLinkId');">LINK 2</a>
<a href="someaction" onClick="removeLinks('removeLinkId');">LINK 3</a>
<a href="someaction" onClick="removeLinks('removeLinkId');">LINK 4</a>
<a href="someaction" onClick="removeLinks('removeLinkId');">LINK 5</a>
</span>
HTML:
<a href="www.google.se" id="foo">LINK 1</a>
Javascript:
//Get the element you need.
var a = getElementById("foo");
//Remember to use the REFERENCE, not actually CALLING the callback.
a.onclick = callback;
function callback() {
//a.setAttribute("style", "display:none;");
a.removeAttribute("href");
return false; //Stop the event (don't navigate
}
Javascript JQUERY:
//Get the element and bind it in one "line".
var a = $("a#foo);
a.bind(
"click",
function() {
a.hide();
a.removeAttr("href");
//Stop the event from bubbling
return false;
});