★★★★★★★★★★★★★★★JS的调用技巧★★★★★★★★★★★★★★★★★★★
如何给文本框加JS
法一:在HTML视图找到相关元素,直接嵌入相关的事件和JS代码。
例如:
设计时候:
<script languag="javascript">
function doFocus(txt) {
txt.value = "";
}
</script>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 8 <title></title> 9 <script languag="javascript"> 10 function doFocus(txt) { 11 txt.value = ""; 12 } 13 14 </script> 15 </head> 16 <body> 17 <form id="form1" runat="server"> 18 <div> 19 20 用户名:<asp:TextBox ID="TextBox1" οnfοcus="doFocus(this)" runat="server" ForeColor="#999999">(必填)</asp:TextBox> 21 <br /> 22 密码:<asp:TextBox ID="TextBox2" runat="server" ForeColor="#999999">(必填)</asp:TextBox> 23 <br /> 24 <asp:Button ID="Button1" runat="server" Text="Button" /> 25 26 <br /> 27 <br /> 28 <br /> 29 <br /> 30 <asp:HyperLink ID="HyperLink1" runat="server" ImageUrl="http://www.itnba.com/upLoad/slide/month_1502/201502261338075041.png" NavigateUrl="http://www.itNBA.com" Target="_blank">汉企人才定制中心</asp:HyperLink> 31 <br /> 32 <br /> 33 <br /> 34 <asp:Image ID="Image1" runat="server" ImageUrl="http://www.itnba.com/upLoad/slide/month_1502/201502261338075041.png" /> 35 36 </div> 37 </form> 38 </body> 39 </html>
用户名:<asp:TextBox ID="TextBox1" οnfοcus="doFocus(this)" runat="server" ForeColor="#999999">(必填)</asp:TextBox>
运行起来:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
用户名:<input name="TextBox1" type="text" value="(必填)" id="TextBox1" οnfοcus="doFocus(this)" style="color:#999999;" />
法二:在aspx.cs文件的Page_Load方法中,使用Attributes属性加入JS
例如:
<script languag="javascript">
function doFocus(txt) {
txt.value = "";
}
</script>
密码:<asp:TextBox ID="TextBox2" runat="server" ForeColor="#999999">(必填)</asp:TextBox>
C#代码:
protected void Page_Load(object sender, EventArgs e)
{
TextBox2.Attributes.Add("onfocus","doFocus(this)");
}
运行起来:
密码:<input name="TextBox2" type="text" value="(必填)" id="TextBox2" οnfοcus="doFocus(this)" style="color:#999999;" />