.focusin()


.focusin( handler(eventObject) )返回: jQuery

描述: 将一个事件函数绑定到 "focusin" 事件。

  • 添加的版本: 1.4.focusin( handler )

    • handler
      类型: Function( Event eventObject )
      每次事件触发时会执行的函数。
  • 添加的版本: 1.4.3.focusin( [eventData ], handler )

    • eventData
      类型: Anything
      一个对象,它包含的数据键值对映射将被传递给事件处理程序。
    • handler
      类型: Function( Event eventObject )
      每次事件触发时会执行的函数。
  • 添加的版本: 1.0.focusin()

    • 这个方法不接受任何参数。

这个方法的前两个用法是 .bind('focusin', handler) 的快捷方式。第三个不带参数的用法是 .trigger( "focusin" ) 的快捷方式。

focusin 事件会在元素(或者其内部的任何元素)获得焦点时触发。这跟 focus 事件的显著区别在于,它可以在父元素上检测子元素获得焦点的情况(换而言之,它支持事件冒泡)。

此事件通常会和focusout事件一起使用。

Additional Notes(其他注意事项):

  • .focusin()方法只是作为.on( "focusin", handler )的一个速记写法,移除该事件可以使用.off( "focusin" )

例子:

监控页面上段落内获得焦点的情况。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<head>
<style>span {display:none;}</style>
<script src="//code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p><input type="text" /> <span>focusin fire</span></p>
<p><input type="password" /> <span>focusin fire</span></p>
<script>
$("p").focusin(function() {
$(this).find("span").css('display','inline').fadeOut(1000);
});
</script>
</body>
</html>

Demo: