In jquery, you can judge whether the specified object has gained focus by judging whether "$(specified object).is(":focus")" is equal to true; the specific syntax format is "if($("selector ").is(":focus")==true){//Get focus}".
The operating environment of this tutorial: windows7 system, jquery3.3.1 version, Dell G3 computer.
Recommended tutorial: jquery video tutorial
Instance of jquery judging whether the object has gained focus
<input type="text" id="input"> <script> // 延迟2s判断输入框是否具有焦点 setTimeout(()=>{ if($("#input").is(":focus")==true){ console.log('focus') }else{ console.log('blur') } }, 2000) </script>
Description:
is() method is used to check whether the selected element matches the selector. The :focus selector selects the focused element.
When the object has focus, $(selector).is(":focus") will return true, otherwise it will return false.
For more programming-related knowledge, please visit: Programming Video! !
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!