I have this simple code:
<html>
<body>
<form name="f1">
<asp:Label name="lbl" runat="server" Text="Label" onclick="lblClick()"></asp:Label>
</form>
<script type="text/javascript">
function lblClick(){
document.f1.lbl.text="new text";}
</script>
</body>
</html>
It doesn't work, it gives me: Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object ... , I 开发者_运维问答can't even change the label text ....why?!!!
<asp:Label id="lbl" name="lbl" runat="server" Text="Label" onclick="lblClick(this)"></asp:Label>
function lblClick(elem)
{
elem.innerText = "new text"; //IE
//elem.textContent = "new text"; //FF
}
let us remember that asp.net renames your controls for you.
<form name="f1">
<asp:Label id="lbl" runat="server" Text="Label" onclick="lblClick()"></asp:Label>
</form>
<script type="text/javascript">
function lblClick(){
document.getElementById('<%=lbl.ClientId %>').innerHTML="new text";}
</script>
Adamantium's answer will also provide you the functionality you're after.
精彩评论