zoukankan      html  css  js  c++  java
  • 文本框获取和失去焦点默认值问题

    1. HTML控件<input id="txtName" type="text" value="默认值" />

    <script src="script/jquery-1.7.1.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function () {
                $("#txtName").focus(function () {
                    if ($(this).val() == "默认值") {
                        $(this).val("");
                    }
                }).blur(function () {
                    if ($(this).val() == "") {
                        $(this).val("默认值");
                    }
                })
            });
        </script>

    2.服务器控件.<asp:TextBox> 直接写在客户端上:

        <asp:TextBox ID="txtName" runat="server" Text="默认值" OnFocus="javascript:if(this.value=='默认值') {this.value=''}"
          OnBlur="javascript:if(this.value==''){this.value='默认值'}">
      </asp:TextBox>


    3.服务器控件写到C#服务器端上:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Script.Serialization;
    
    namespace Web
    {
        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='默认值'}");
            }
        }
    }

    总结. 单个文本框设置默认值时

  • 相关阅读:
    GCC 里面的一些命令
    Servlet中的GET和POST之间的区别
    短暂的计算机职业生涯
    如何写详细设计文档
    exe4j打包jar文件成exe .
    android反编译问题
    OnScrollListener拖住主线程
    涵盖Android所有知识点的小实例
    屏蔽log
    sdk调低版本时,clean后失去R文件
  • 原文地址:https://www.cnblogs.com/gxmaspx/p/3238154.html
Copyright © 2011-2022 走看看