Javascript 如何在asp.net c中显示和隐藏mouseover和mouseout事件上的标签#

Javascript 如何在asp.net c中显示和隐藏mouseover和mouseout事件上的标签#,javascript,asp.net,Javascript,Asp.net,如果我的鼠标在Label1上,我想让Label2靠近它。然后,当我的鼠标离开Label1时,我想隐藏Label2。我尝试了以下代码。我的javascript很差 <script language="javascript" type="text/javascript"> function LabelHover() { document.getElementById('Label1').style.display = 'inherit';

如果我的鼠标在Label1上,我想让Label2靠近它。然后,当我的鼠标离开Label1时,我想隐藏Label2。我尝试了以下代码。我的javascript很差

    <script language="javascript" type="text/javascript">
    function LabelHover()
    {
        document.getElementById('Label1').style.display = 'inherit';
    }

    function Labelleave() 
    {
        document.getElementById("Label1").style.display='none';
    }
    </script>

</head>
<body>
    <asp:Label ID="Label1" runat="server" Text="Hello" Height="120" Width= "120" ForeColor="Brown" style=" left:220px; border:groove; top:15px " >
    </asp:Label>

    <asp:Label ID="Label2" runat="server" Text="Disclaimer" Height="17" Width= "100" ForeColor="Brown" onmouseover="LabelHover()" onmouseout="Labelleave()" style=" left:220px; " >
    </asp:Label>

函数LabelHover()
{
document.getElementById('Label1').style.display='inherit';
}
函数Labelleave()
{
document.getElementById(“Label1”).style.display='none';
}
以下是我的javascript:

<script language="javascript" type="text/javascript">
    function LabelHover() {
        document.getElementById('<%= Label1.ClientID%>').style.display = 'inherit';
    }

    function Labelleave() {
        document.getElementById('<%= Label1.ClientID%>').style.display = 'none';
    }
</script>

函数LabelHover(){
document.getElementById(“”).style.display='inherit';
}
函数Labelleave(){
document.getElementById(“”).style.display='none';
}
和标记:

<asp:Label ID="Label1" runat="server" Text="Hello" Height="120" Width= "120" ForeColor="Brown" style=" left:220px; border:groove; top:15px; float:left; " >
</asp:Label>

<asp:Label ID="Label2" runat="server" Text="Disclaimer" Height="17" Width= "100" ForeColor="Brown" onmouseover="LabelHover()" onmouseout="Labelleave()" style=" left:220px;float:left; " >
</asp:Label>

既然要显示或隐藏Label2依赖于Label1,那么必须在Label1而不是Label2添加onmouseover()和onmouseout()

<asp:Label ID="Label1" runat="server" Text="Hello" Height="120" Width= "120" onmouseover="LabelHover()" onmouseout="Labelleave()" ForeColor="Brown" style=" left:220px; border:groove; top:15px " >
    </asp:Label>

    <asp:Label ID="Label2" runat="server" Text="Disclaimer" Height="17" Width= "100" ForeColor="Brown"  style=" left:220px; " >
    </asp:Label>

Javascript:

<script language="javascript" type="text/javascript">
    function LabelHover() {
        document.getElementById('Label2').style.visibility = 'visible';
    }

    function Labelleave() {
        document.getElementById('Label2').style.visibility = 'hidden';
    }
    </script>

函数LabelHover(){
document.getElementById('Label2')。style.visibility='visible';
}
函数Labelleave(){
document.getElementById('Label2')。style.visibility='hidden';
}

标签的ID是否实际设置为Label1和Label2?在Chrome/Firefox中检查它们并验证它们是否正确。我这样问是因为您可能遇到了ASP.NET客户端ID遗留命名方案。另外,您是否验证了正在调用LabelHover/LabellAve(通过添加警报/控制台输出或在浏览器中使用javascript断点)?getElementById('Label2')不是一个好的做法,因为标签id将从服务器生成,如果它位于contentplaceholder中,则类似于“contentplaceholder 1\u Label2”而不是“Label2”,将导致javascript错误。我同意如果他/她创建一个用户控件并将其注册到aspx页面,但如果他/她在aspx页面中添加这些标签,则不需要客户端。您必须同意,如果他/她使用任何母版页,它也不会起作用,对吗?当然,如果该标签不在同一页面中,则是的(usercontrol、aspx、master page)作为javascript函数,您需要clientID,但如果它位于同一页面中,则根本不需要它