Home  >  Article  >  Web Front-end  >  How to determine whether an object has focus in jquery

How to determine whether an object has focus in jquery

青灯夜游
青灯夜游Original
2020-12-23 14:55:293738browse

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}".

How to determine whether an object has focus in jquery

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(&#39;focus&#39;)
       }else{  
           console.log(&#39;blur&#39;)
       }
   }, 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn