用jquery实现输入框消失文字 .

  1. <html>                                                                                                                        
  2.   <head>                                                                                                                      
  3.     <title>input test</title>                                                                         
  4.     <meta name="Content-Type" content="text/html; charset=UTF-8" />                                                           
  5. <style type="text/css">                                                                                                       
  6. //这里放置css    
  7. </style>    
  8.                                                                                                                     
  9. <script type="text/javascript" src="js/jquery.js"></script>                                                               
  10. <script type="text/javascript">                                                                                               
  11. //这里放置jquery代码  
  12. </script>                                                                                                                     
  13.     
  14.   </head>                                                                                                                     
  15.     
  16. <body>                                                                                                                        
  17.     <form method="POST" id="user" action="">                                                                                  
  18.       User Name:<input type="text"  name="username" value="Enter your name" /><br/>                                           
  19.       PassWord:<input type="password"  name="password" value="Enter your password" />                                         
  20.       <input type="submit" name="sub" value="login" />                                                                      
  21.       </form>   
  22.   </div>                                                                                                                      
  23. </body>                                                                                                                       
  24.     
  25. </html>   
<html>                                                                                                                      
  <head>                                                                                                                    
    <title>input test</title>                                                                       
    <meta name="Content-Type" content="text/html; charset=UTF-8" />                                                         
<style type="text/css">                                                                                                     
//这里放置css  
</style>  
                                                                                                                  
<script type="text/javascript" src="js/jquery.js"></script>                                                             
<script type="text/javascript">                                                                                             
//这里放置jquery代码
</script>                                                                                                                   
  
  </head>                                                                                                                   
  
<body>                                                                                                                      
    <form method="POST" id="user" action="">                                                                                
      User Name:<input type="text"  name="username" value="Enter your name" /><br/>                                         
      PassWord:<input type="password"  name="password" value="Enter your password" />                                       
      <input type="submit" name="sub" value="login" />                                                                    
      </form> 
  </div>                                                                                                                    
</body>                                                                                                                     
  
</html> 

下面加入jquery代码:

我使用了click 和blur内置事件类型处理,而且,只是对username框有效(因为密码框还有别的因素考虑)

  1. <html>  
  2.   <head>  
  3.     <title>input test</title>  
  4.     <meta name="Content-Type" content="text/html; charset=UTF-8" />  
  5. <style type="text/css">  
  6.   
  7. </style>  
  8.     <script type="text/javascript" src="js/jquery.js"></script>  
  9. <script type="text/javascript">  
  10. $(document).ready(function(){  
  11. $("#username").click(  
  12.     function(){  
  13.     if($(this).val()=="Enter your name"){  
  14.     $(this).val("");  
  15.     }  
  16.       
  17. })  
  18. $("#username").blur(  
  19.     function(){  
  20.     if($(this).val()=="")  
  21.     {  
  22.     $(this).val("Enter your name");  
  23.     }  
  24. })  
  25. });  
  26. </script>  
  27.   </head>  
  28. <body>  
  29.   <div id="content">  
  30.     <form method="POST" id="user" action="">  
  31.       User Name:<input type="text" id="username" name="username" value="Enter your name" /><br/>  
  32.       PassWord:<input type="password"  name="password" value="Enter your password" />  
  33.         <input type="submit" name="sub" value="login" />  
  34.     </form>  
  35.   </div>  
  36. </body>  
  37. </html>  
<html>
  <head>
    <title>input test</title>
    <meta name="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">

</style>
    <script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#username").click(
    function(){
    if($(this).val()=="Enter your name"){
    $(this).val("");
    }
    
})
$("#username").blur(
    function(){
    if($(this).val()=="")
    {
    $(this).val("Enter your name");
    }
})
});
</script>
  </head>
<body>
  <div id="content">
    <form method="POST" id="user" action="">
      User Name:<input type="text" id="username" name="username" value="Enter your name" /><br/>
      PassWord:<input type="password"  name="password" value="Enter your password" />
        <input type="submit" name="sub" value="login" />
    </form>
  </div>
</body>
</html>

2.做的更好

这样基本的原型就写成了,但是这个原型有许多的不足:
1.也许可以对密码框也使用这种方式,但是密码框的type类型是password,它不能显示,何来提示文字?
2. if($(this).val()=="")这种写法我可以接受,但是 if($(this).val()=="Enter your name"),你不觉得这很...要是我就想输这个呢...
3.提示文字用别的灰色的粗体表示,这样交互性是不是更强?
4.既然想要用两种字体表示,能不能把他们提取出来?写在.css里?这个是可以重用的啊!
解决办法:
1.密码框先让它的type是text的,等到点击了,我们再设置成password
2.用个变量来表示是否要切换吧。
3.设置不同的css.
4.用attr("class","class1"),attr("class","class2")来切换class,而不是引用id.(也就是说用.不用#)
下面是实现:

  1. <html>  
  2.   <head>  
  3.     <title>input test</title>  
  4. <style type="text/css">  
  5. .default {  
  6. font-weight:bold;  
  7. color:#787878;  
  8. }  
  9. .puton{  
  10. font-weight:normal;  
  11. color:black;  
  12. }  
  13. </style>  
  14.     <script type="text/javascript" src="jquery.js"></script>  
  15. <script type="text/javascript">  
  16.   
  17. $(document).ready(function(){  
  18. var b=true;  
  19. $("#username").click(  
  20.     function(){  
  21.         if(b==true){  
  22.         $(this).val("");  
  23.         $(this).attr("class","puton");  
  24.         b=false;  
  25.         }  
  26.         }  
  27.     )  
  28.  $("#username").blur(  
  29.     function(){  
  30.     if( $(this).val()==""){  
  31.         $(this).val("Enter your name");  
  32.         $(this).attr("class","default");  
  33.         b=true;  
  34.     }  
  35. }  
  36. )  
  37. });  
  38.   
  39. $(document).ready(function(){  
  40. var b=true;  
  41. $("#password").click(  
  42.     function(){  
  43.     if(b==true){  
  44.     $(this).val("");  
  45.     $(this).attr("type","password");  
  46.     $(this).attr("class","puton");  
  47.     b=false;  
  48.     }  
  49. })  
  50. $("#password").blur(  
  51.     function(){  
  52.     if( $(this).val()==""){  
  53.         $(this).val("Enter your password");  
  54.         $(this).attr("type","text");  
  55.         $(this).attr("class","default");  
  56.         b=true;  
  57.     }  
  58. }  
  59. )  
  60. });  
  61.   
  62.   
  63. </script>  
  64.   </head>  
  65. <body>  
  66.   <div id="content">  
  67.     <form method="POST" id="user" action="">  
  68.       User Name:<input type="text" id="username" class="default" name="username" value="Enter your name" /><br/>  
  69.       PassWord:<input type="text" id="password"  class="default" name="password" value="Enter your password" />  
  70.         <input type="submit" name="sub" value="login" />  
  71.     </form>  
  72.   </div>  
  73. </body>  
  74. </html>  
<html>
  <head>
    <title>input test</title>
<style type="text/css">
.default {
font-weight:bold;
color:#787878;
}
.puton{
font-weight:normal;
color:black;
}
</style>
    <script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">

$(document).ready(function(){
var b=true;
$("#username").click(
    function(){
        if(b==true){
        $(this).val("");
        $(this).attr("class","puton");
        b=false;
        }
        }
    )
 $("#username").blur(
    function(){
    if( $(this).val()==""){
        $(this).val("Enter your name");
        $(this).attr("class","default");
        b=true;
    }
}
)
});

$(document).ready(function(){
var b=true;
$("#password").click(
    function(){
    if(b==true){
    $(this).val("");
    $(this).attr("type","password");
    $(this).attr("class","puton");
    b=false;
    }
})
$("#password").blur(
    function(){
    if( $(this).val()==""){
        $(this).val("Enter your password");
        $(this).attr("type","text");
        $(this).attr("class","default");
        b=true;
    }
}
)
});


</script>
  </head>
<body>
  <div id="content">
    <form method="POST" id="user" action="">
      User Name:<input type="text" id="username" class="default" name="username" value="Enter your name" /><br/>
      PassWord:<input type="text" id="password"  class="default" name="password" value="Enter your password" />
        <input type="submit" name="sub" value="login" />
    </form>
  </div>
</body>
</html>

3.更多:
把css写到外部文件.

DRY原则!用插件来实现.

顺便说一下,我一直用的ubuntu,firefox,chrome都测试过,IE下确实没测试过,有人给我留言说IE下行不通,我想想解决办法吧,不要喷了...

初学jquery..这是学习笔记...

我在下一篇博客去完善.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值