ASP.NET TextBox 当鼠标点击后清空默认提示文字
[ 方法一]
前台代码:
<div>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox>
<asp:Button ID="btnLongin" runat="server" Text="提交" />
</div>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//用户输入
txtName.Attributes.Add("Value", "请输入用户名");
txtName.Attributes.Add("OnFocus", "if(this.value=='请输入用户名') {this.value=''}");
txtName.Attributes.Add("OnBlur", "if(this.value==''){this.value='请输入用户名'}");
//密码输入
txtPwd.Attributes.Add("Value", "请输入密码");
txtPwd.Attributes.Add("OnFocus", "if(this.value=='请输入密码'){this.value=''}");
txtPwd.Attributes.Add("OnBlur", "if(this.value==''){this.value='请输入密码'}");
//
if (!IsPostBack)
{
//内容
}
}
}
[ 方法二]
前台文本框里添加下面2个事件属性:
OnFocus="javascript:if(this.value=='提示文字') {this.value=''}"
OnBlur="javascript:if(this.value=='') {this.value='提示文字'}"
-----------------------------------例-----------------------------------------
<asp:TextBox ID="txtName" runat="server" Text="请输入用户名"
OnFocus="javascript:if(this.value=='请输入用户名') {this.value=''}"
OnBlur="javascript:if(this.value==''){this.value='请输入用户名'}">
</asp:TextBox>
<asp:TextBox ID="txtPwd" runat="server" Text="请输入密码"
OnFocus="javascript:if(this.value=='请输入密码') {this.value=''}"
OnBlur="javascript:if(this.value==''){this.value='请输入密码'}">
</asp:TextBox>