使用javascript禁用链接 [英] Disable link using javascript
问题描述
我有以下HTML,并希望使用javascript禁用链接。
I have following HTML and would like to disable the link using javascript.
<a style="white-space: nowrap;" onclick="return InstallWebApp(true);" id="uc_ii_lnkInstall" href="javascript:__doPostBack('uc_ii$lnkInstall','')">
<img style="border-width: 0pt; margin-right: 3px;" id="uc_ii_lnkInstallImg" alt="Install" title="Install" src="/CortexDotNet/pics/buttons/install_g.gif">
Install
</a>
我使用的JavaScript是:
The JavaScript I am using are :
document.getElementById("uc_ii_lnkInstall").disabled = true;
但是,它不起作用,我仍然可以点击此链接,我已经禁用链接使用上面的javascript.I看看html,它似乎没有在一个标签的禁用属性。任何人可以帮助我解释这个吗?
However , it does not work , I could still click this this link after I have disabled the link using the above javascript.I look at the html , it does not seem to have the disable attribute in the a tag.Can anyone help me to explain this please?
推荐答案
document.getElementById("uc_ii_lnkInstall").onclick = function() { return false; };
旧式事件处理程序中的false返回值会阻止默认动作(即加载javascript :URL)。
The return value of false in the old-style event handler prevents the default action (i.e. loading the javascript: URL).
如果你想灰色的图像链接,你还需要交换图像的src URL,一个指向一个灰色的版本图标并使用 .style.color =gray;
。
If you want to gray out the image link, you would also need to swap out the image's src URL with one pointing to a grayed-out version of the icon and change the text's color using .style.color = "gray";
.
这篇关于使用javascript禁用链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!