jQuery 文字框得失焦點應用

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   java   strong   sp   

一、文字框得失焦點一種是改變文字框的樣式
    得到焦點:

         
    失去焦點:

          
二、文字框得失焦點另一種是改變文字框的值
    得到焦點:

          
    失去焦點:

     

三、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 文字框得失焦點應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.