TextBox獲得焦點——客戶端JavaScript

.aspx

    <div>
        
<asp:TextBox ID="txtUser" runat="server" Text="請輸入用戶名"></asp:TextBox>
        
<br />
        
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password" ></asp:TextBox>  
    
</div>

.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    
{
        txtUser.Attributes.Add(
"OnFocus""if(this.value=='請輸入用戶名') {this.value=''}");
        txtUser.Attributes.Add(
"OnBlur""if(this.value==''){this.value='請輸入用戶名'}");
        txtPwd.Attributes.Add(
"Value""請輸入密碼");
        txtPwd.Attributes.Add(
"OnFocus""if(this.value=='請輸入密碼'){this.value=''}");
    }
實現效果和HTML控件效果
<input type="text" value="請輸入用戶名" onfocus="if(value=='請輸入用戶名') {value=''}" onblur="if (value=='') 
{value='請輸入用戶名'}"
/>
<br/>
<input type="password" value="請輸入密碼" onfocus="if(value=='請輸入密碼') {value=''}"
    onBlur
="if(value=='') {value='請輸入密碼'}"/>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章