清空 input 框 text 里面的 value值

本文介绍了如何使用原生JavaScript和jQuery为单个及多个输入框设置默认提示文字,并在获得焦点时清除这些文字,在失去焦点且为空时恢复默认提示。
  1. 单个input框的情况下:
    window.onload= function() {
        var txt1=document.getElementByIdx_x_x("txt1"),dv="请输入";
        txt1.value=dv;
        txt1.onfocus= function() {
       //获取焦点时如果value跟默认值相同,清空文本框内容
        if(this.value==dv)
        this.value="";
        };
        txt1.onblur= function() {
       //失去焦点时,如果没有内容还显示默认值
        if(this.value=="")
        this.value=dv;
        };
    }
  2. 多个input框的情况下:
     <html>
         <head>
         <title>test</title>
         <script src="jquery.js"></script>
         </head>
         <body>
         <form id="form" action="" method="get">
             <input type="text" name="First name" value="First name"/>
             <input type="text" name="Family name" value="Family name"/>
             <input type="text" name="Email" value="Email..."/>
             <input type="text" name="password" value="password..."/>
             <input type="text" name="location" value="City where you 
    
    live"/>
             <input type="submit" name="sign up" value="Sign Up"/>
          </form>
     
          <script>
             $('#form').children(':text').focus(function(){
               if(!this.initValue){
              this.initValue = this.value;
              }
               if(this.value === this.initValue){
               this.value = '';
              }
           }).blur(function(){
               if(this.value === '' || this.value === null){
               this.value = this.initValue;
              }
           });
          </script>
        </body>
        </html>
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值