Home > Article > Web Front-end > How to determine whether an object has focus in jquery
Jquery method to determine whether an object has gained focus: first create a code sample file; then use the is() method to check whether the selected element matches the selector; finally, combine it with the CSS3 pseudo-class selector ":focus" that is Can determine whether the object has gained focus.
The operating environment of this article: windows7 system, jquery1.2.6 version, Dell G3 computer.
jquery can use the is() method to determine whether an object has focus. The is() method is used to check whether the selected element matches the selector. It can be judged by combining the CSS3 pseudo-class selector: focus. The specific code is $(selector).is(':focus'), when the return result is true, it has focus.
How does jquery determine whether an object has focus?
jquery can use $(selector).is(":focus") to determine whether an object has gained focus. Returns true when the object has focus, false otherwise.
Syntax: $(selector).is('')
Function: The is() method is used to check whether the selected element matches the selector.
:The focus selector is used to select the element that receives focus.
Tip: The :focus selector is allowed on elements that receive keyboard events or other user input.
Recommended: "jquery video tutorial"
Usage example:
<input type="text" id="input"> <script> // 延迟2s判断输入框是否具有焦点 setTimeout(()=>{ var isFocus=$("#input").is(":focus"); if(true==isFocus){ console.log('focus') }else{ console.log('blur') } }, 2000) </script>
After refreshing the web page, click the input box immediately and the program will output focus
After refreshing the web page, the program will output blur if the input box is not clicked.
The above is the detailed content of How to determine whether an object has focus in jquery. For more information, please follow other related articles on the PHP Chinese website!