focusin 事件


將事件處理常式繫結到「focusin」事件,或在元素上觸發該事件。

.on( "focusin" [, eventData ], handler )傳回:jQuery

說明:將事件處理常式繫結到「focusin」事件。

此頁面說明 focusin 事件。如需已棄用的 .focusin() 方法,請參閱 .focusin()

當元素或其內部的任何元素獲得焦點時,focusin 事件會傳送給元素。這與 focus 事件不同,在於它支援偵測父元素上的焦點事件(換句話說,它支援事件冒泡)。

此事件可能會與 focusout 事件一起使用。

範例

監看焦點發生在頁面上的段落中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>on demo</title>
<style>
span {
display: none;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
<p><input type="text"> <span>focusin fire</span></p>
<p><input type="password"> <span>focusin fire</span></p>
<script>
$( "p" ).on( "focusin", function() {
$( this ).find( "span" ).css( "display", "inline" ).fadeOut( 1000 );
} );
</script>
</body>
</html>

示範