我有一些链接需要处理几秒钟后才能发送响应,因此我想实现一个指示图标。以下是我的起点:
<a href="/foo"><img src="icon.png" /></a>
这是一个遗留的应用程序,代码已经很乱了,所以我不介意并使用内联事件处理程序:
<a href="/foo"><img src="icon.png" onclick="indicate(this)" /></a>
此外,没有JS框架绑定。我可以使用其他机制来应用事件处理程序,但这不会改变我要解决的问题。
由于后端处理消耗了大量资源,我想防止用户多次点击。我尝试在第一次单击时删除href属性。似乎通过使用延迟计时器,在发送请求后正确地删除了href,但是Firefox和IE9都允许我再次单击链接。
以下是“indicate()”函数:
function indicate(e) {
if (indicator.ref.nodeName) indicateStop();
// save state
indicator.ref = e;
indicator.orig.href = indicator.ref.parentNode.href;
indicator.orig.src = indicator.ref.src;
// replace icon
indicator.ref.src = indicator.src;
// remove href
setTimeout(function(){ indicator.ref.parentNode.removeAttribute("href"); }, 20);
}
所以问题是,如何通过点击来取消链接(锚点)的“可点击性”?
event.preventDefault
,并自行处理它(当您禁用链接时,您还应该将光标外观更改为default
)。 - steveax