码迷,mamicode.com
首页 > Web开发 > 详细

jQuery 文本框得失焦点应用

时间:2014-10-13 19:00:07      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   java   strong   sp   

一、文本框得失焦点一种是改变文本框的样式
    得到焦点:

          bubuko.com,布布扣
    失去焦点

           bubuko.com,布布扣
二、文本框得失焦点另一种是改变文本框的值
    得到焦点:

          bubuko.com,布布扣
    失去焦点:

     bubuko.com,布布扣

三、jQuery 得失焦点代码

     1、改变文本框样式代码

      1> CSS代码  

     .focus { 
         border: 1px solid #f00;
         background: #fcc;
      } 


2>jQuery代码 (:input匹配 所有 input, textarea, select 和 button 元素)
    <script type="text/javascript">
    $(function(){
        $(":input").focus(function(){
              $(this).addClass("focus");
        }).blur(function(){
              $(this).removeClass("focus");
        });
    })
    </script>

     2、改变文本框值的代码

       1>jQuery代码

       用:input匹配所有的input元素,当获取焦点时,就添加样式focus,通过$(this)自动识别当前的元素。

       focus()方法是获取焦点事件发生时执行的函数。

       blur()方法是失去焦点事件发生时执行的函数。

    <script type="text/javascript">
       $(function(){
        $(":input").focus(function(){
              $(this).addClass("focus");
              if($(this).val() ==this.defaultValue){  
                  $(this).val("");           
              } 
        }).blur(function(){
             $(this).removeClass("focus");
             if ($(this).val() == ‘‘) {
                $(this).val(this.defaultValue);
             }
        });
    })
    </script>

 

jQuery 文本框得失焦点应用

标签:style   blog   http   color   io   ar   java   strong   sp   

原文地址:http://www.cnblogs.com/1312mn/p/4022636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!