很多时候 为了编程方便、快捷,我们经常定义全局变量 页面加载初始化并赋值
但是在此全局变量为控件对象时:
var test;
$(function ()
{
test = document.getElementById("TextBox1");
});
function clk()
{
alert(test.value);
}
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="containerDiv">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
<input id="Button1" type="button" value="button" onclick="clk()" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = "abc";
}
再触发clk()点击事件时 弹出的将会还是空
所以对于控件对象 还是何时用何时getElementByXXX()得好