zoukankan      html  css  js  c++  java
  • jQuery 文本框得失焦点应用

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

             
        失去焦点

              
    二、文本框得失焦点另一种是改变文本框的值
        得到焦点:

              
        失去焦点:

         

    三、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>
    
  • 相关阅读:
    LeetCode-160. Intersection of Two Linked Lists
    LeetCode-328. Odd Even Linked List
    LeetCode-234. Palindrome Linked List
    Trie树
    圆圈中最后剩下的数字
    LeetCode-215. Kth Largest Element in an Array
    n个骰子的和
    DFS & BFS
    ubuntu14.04安装显卡驱动(转载)
    Ubuntu 14.04 配置caffe环境
  • 原文地址:https://www.cnblogs.com/1312mn/p/4022636.html
Copyright © 2011-2022 走看看