• jquery得到焦点和失去焦点


    鼠标在搜索框中点击的时候里面的文字就消失了,经常会用到搜索框的获得焦点和失去焦点的事件,接下来介绍一下具体代码,感兴趣的朋友额可以参考下
     
    input失去焦点和获得焦点 
    鼠标在搜索框中点击的时候里面的文字就消失了。 
    我们在做网站的时候经常会用到搜索框的获得焦点和失去焦点的事件,因为懒,每次都去写非常的烦,于是就一劳永逸,遇到类似情况就来调用一下就OK 了 

    相关js代码: 
    复制代码代码如下:

    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>input失去焦点和获得焦点jquery焦点事件插件 - 懒人建站</title> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
    $(document).ready(function(){ 
    //focusblur 
    jQuery.focusblur = function(focusid) { 
    var focusblurid = $(focusid); 
    var defval = focusblurid.val(); 
    focusblurid.focus(function(){ 
    var thisval = $(this).val(); 
    if(thisval==defval){ 
    $(this).val(""); 

    }); 
    focusblurid.blur(function(){ 
    var thisval = $(this).val(); 
    if(thisval==""){ 
    $(this).val(defval); 

    }); 
    }; 
    /*下面是调用方法*/ 
    $.focusblur("#searchkey"); 
    }); 
    </script> 
    </head> 
    <body> 
    <form action="" method="post"> 
    <input name="" type="text" value="输入搜索关键词" id="searchkey"/> 
    <input name="" type="submit" id="searchbtn" value="搜索"/> 
    </form> 
    <p>input失去焦点和获得焦点jquery焦点事件插件,<br/><strong style="color:#F00">鼠标在搜索框中点击的时候里面的文字就消失了</strong>。</p> 
    </body> 
    </html> 

    jquery获取和失去焦点事件 
    复制代码代码如下:

    <script src="jquery-1.9.1.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
    $(function () { 
    $('#username').focus(function ()//得到教室时触发的时间 

    $('#username').val(''); 
    }) 
    $('#username').blur(function () 失去焦点时触发的时间 

    if ($('#username').val() == 'marry') { 
    $('#q').text('用户名已存在!') 

    else { $('#q').text('ok!') } 
    }) 
  • 相关阅读:
    js中的投掷筛子的小游戏
    js俩习题
    python中socket理论
    python实现计时器(装饰器)
    python之正则表达式(re模块)用法总结
    python练习题之随机生成验证码
    python中模块介绍
    python中的装饰器基本理论
    python类与对象练习题扑克牌
    Java抓取网页数据
  • 原文地址:https://www.cnblogs.com/liuwenbohhh/p/4344955.html
Copyright © 2020-2023  润新知