如何在javascript中清除标签文本或使标签不可见

问题描述:

大家好,



我的标签定义如下:

< asp:Label ID =lblWarningrunat = serverForeColor =RedVisible =FalseHeight =19pxWidth =276px>



我试图清除标签文字像这样:

hi All,

I have a label defined like this:
<asp:Label ID="lblWarning" runat="server" ForeColor="Red" Visible="False" Height="19px" Width="276px">

I tried to clear the text of a label like this :

function ChangeProperty() {
            var b = document.getElementById('<%=lblWarning.ClientID%>');
if (b!=null)

            {
                b.innerText = "";
}

     }







这不起作用。请帮忙。



谢谢。




This is not working.Please help.

Thanks.

当您将服务器端控件的Visible属性设置为假它甚至没有呈现给客户端,所以你的代码根本找不到它(b将为null)...

如果你想让它在客户端上可见/不可见你必须使用客户端属性...

设置显示或清除显示属性...

When you set the Visible property of a server side control to false it not even rendered to the client, so your code will not found it at all (b will be null)...
If you want to make it visible/invisible on the client you have to play with client side attributes...
Set display or clear the display attribute...
<asp:label id="lblWarning" runat="server" forecolor="Red" height="19px" width="276px" xmlns:asp="#unknown"></asp:label>




function ChangeProperty() {
  var b = document.getElementById('<%=lblWarning.ClientID%>');
  if (b!=null)
  {
    if(b.style.display == "none")
    {
      b.style.display = "";
    }
    else
    {
      b.style.display = "none";
    }
  }
}