我的表单上有一个标签,如下所示:
<asp:Label ID="lblPwPol"
runat="server"
Visible="false">
A bunch of text...
</asp:Label>
在 Page_Load
事件 (VB.net) 调用的方法中,我附加了一些 JS 事件处理程序:
btnPasswordPolicies.Attributes.Add("onClick", "return mShowToolTip();")
cmdPasswordPolicies.Attributes.Add("onmouseleave", "return mHideToolTip();")
定义如下:
function mShowToolTip(aCtrl, aArg1)
{
document.getElementById("lblPwPol").style.display = 'block';
return false;
}
function mHideToolTip()
{
document.getElementById("lblPwPol").style.display = 'none';
return false;
}
当我的表单加载时,标签不可见(正确)。
当我单击标签时,我的 JS 中出现异常:
Javascript runtime error: Unable to get property 'style' of undefined or null reference
这显然意味着 document.getElementById("lblPwPol")
返回 null。
知道我做错了什么吗?我觉得这一定是一些愚蠢的事情,但我根本不懂 JS。
我还尝试将 id="lblPwPol"
改为 name="lblPwPol"
,但这并没有什么区别。
最佳答案
首先,您应该删除标签中的visible="false",因为您将其设置为visible false,因此它不会在Dom中加载。并保持 style="display:none" 像这样
<asp:Label ID="lblPwPol"
runat="server"
Style="display:none">
A bunch of text...
</asp:Label>
关于javascript - 如何切换文档元素的可见性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36321845/