zoukankan      html  css  js  c++  java
  • 通过jquery 获取文本框的聚焦和失焦方法

    我还是喜欢用jquery来实现,不管页面中多少个输入框需要实现聚焦,失焦,都公有,我常用的方法是:

    遍历该页面中的input框,获取输入框中的val值,当该输入框聚焦的时候跟存放的oldValue值进行比较,如果值相同,就把该值变为空,失焦的时候,把原来存放的值再吃赋值到val上,具体代码如下:

    $(function(){
    
        $("input[class*=input]").each(function(){  
           var oldValue=$(this).val();  
           
           $(this).focus(function(){     
            if($(this).val()==oldValue){
                $(this).val('');
                }    
           })
           .blur(function(){
            if($(this).val()==""){
                $(this).val(oldValue)
                } 
           })      
        });
    
    })

    js实现方法一:直接在input中添加

    <input type="text" onfocus="if(this.value=='聚焦吧') this.value='';" onblur="if(this.value=='') this.value='聚焦吧';" value="聚焦吧" name="name">

    js实现方法二:

    html代码

    <input type="text" value="聚焦吧" id="myinput" /> 

    js代码如下:

    function addListener(element,e,fn){ 
            if(element.addEventListener){ 
                 element.addEventListener(e,fn,false); 
            }else{ 
                element.attachEvent("on" + e,fn); 
            } 
        } 
        var myinput = document.getElementById("myinput"); 
        addListener(myinput,"click",function(){ 
            myinput.value = ""; 
        }) 
        addListener(myinput,"blur",function(){ 
            myinput.value = "聚焦吧"; 
        })

    当然当页面中有聚焦,失焦的时候,我还是推荐使用jquery的这种实现方式的。

  • 相关阅读:
    3.db2性能和优化
    SpringBoot之demo
    1设计模式---工厂模式。
    1.添加maven项目后,tomcat启动失败
    2.如何卸载mysql
    2.hdfs中常用的shell命令
    1.在eclipse上添加maven
    2.hive入门篇
    1.hive数据库调优之路
    2.myeclipse的使用技巧
  • 原文地址:https://www.cnblogs.com/good10000/p/4756619.html
Copyright © 2011-2022 走看看