关于JavaScript修改Label的值
我想用JavaScript把Label1赋值为 Hello World,可是这样做,老是不成功,帮帮忙啊。。。
JS文件夹下面的 js文件 test.js:
function writeString(){ document.getElementById("Label1").Value="Hello World"; }<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>无标题页</title> <script type="text/javascript" src="JS/test.js"></script></head><body onload="writeString()"> <form id="form1" runat="server"> <asp:Label ID="Label1" runat="server"></asp:Label> <div> </div> </form></body>
function writeString(){ document.getElementById("Label1").innerHTML="Hello World"; }
[解决办法]
<body onload="writeString()">
顺序不对 调用函数的时候label还没有创建啦
[解决办法]
你这样写,那控件不能是服务器控件的,再说了服务器控件也没VALUE这个属性
[解决办法]
考虑到母版页等的情况,最好的以后习惯用:
document.getElementById('<%= Label1.ClientID %>').innerText = "Hello World";
这种写法。
[解决办法]