jQuery focus()事件指的是鼠标点击输入框等元素时触发的事件,例如<input>、<select> 等表单元素以及链接 <a href>元素。

focus方法经常和blur()方法一起使用。

语法

$(selector).focus(function) 

    参数

    参数说明
    function是可选参数。用于指定元素获得焦点时运行的函数。

    例子

    看一个简单的forcus事件的例子

    <!doctype html>
    <html lang="zh-CN">
    <head>
      <meta charset="utf-8">
      <title>jQuery focus() 事件例子</title>
      <style>
      span {
        display: none;
      }
      </style>
      <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>
     <p>名字:<input type="text"> <span>Focus 开始.. 写下你的名字。</span></p>
    <p>密码:<input type="password"> <span>Focus 开始.. 输入您的密码。</span></p>
     <script>
    $( "input" ).focus(function () {
      $( this ).next( "span" ).css( "display", "inline" ).fadeOut( 2000 );
    });
    </script>
     </body>
    </html>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    效果

    jQuery focus() 事件