Contents:
.on( "focus" [, eventData ], handler )Returns: jQuery
Description: Bind an event handler to the "focus" event.
-
version added: 1.7.on( "focus" [, eventData ], handler )
focus
event. For the deprecated .focus()
method, see .focus()
.
- The
focus
event is sent to an element when it gains focus. This event is implicitly applicable to a limited set of elements, such as form elements (<input>
,<select>
, etc.) and links (<a href>
). In recent browser versions, the event can be extended to include all element types by explicitly setting the element'stabindex
property. An element can gain focus via keyboard commands, such as the Tab key, or by mouse clicks on the element. - Elements with focus are usually highlighted in some way by the browser, for example with a dotted line surrounding the element. The focus is used to determine which element is the first to receive keyboard-related events.
.trigger( "focus" )
on elements that are visible. To run an element's focus event handlers without setting focus to the element, use .triggerHandler( "focus" )
instead of .trigger( "focus" )
.
1
2
3
4
5
6
7
|
|
1
2
3
|
|
1
2
3
|
|
focus
event does not bubble. As of version 1.4.2, jQuery works around this limitation by mapping focus
to the focusin
event in its event delegation methods.
focus
event is asynchronous in all versions of IE, contrary to other browsers. To avoid issues related to this discrepancy, as of jQuery 3.7.0, jQuery uses focusin
as the native backing event for focus
in IE.
Examples:
Example 1
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
|
|
Demo:
Example 2
1
2
3
|
|
Example 3
1
2
3
|
|
.trigger( "focus" )Returns: jQuery
Description: Trigger the "focus" event on an element.
-
version added: 1.0.trigger( "focus" )
-
"focus"Type: stringThe string
"focus"
.
-
.on( "focus", ... )
.