Home  >  Article  >  Web Front-end  >  How to get focus in jQuery?

How to get focus in jQuery?

青灯夜游
青灯夜游Original
2020-11-24 16:48:516452browse

JQuery's method of obtaining focus: You can use the focus method to obtain focus, the syntax "$(selector).focus()"; the focus method can trigger the focus event focus, or specify a function to be run when a focus event occurs .

How to get focus in jQuery?

The operating environment of this tutorial: windows7 system, jquery3.5 version. This method is suitable for all brands of computers.

Related recommendations: "jQuery Video Tutorial"

Focus refers to whether the user is active on the page. Specifically, whether the mouse is active within the web page. Or whether the content in the web page is selected, or the cursor exists in an element of the page. If one of these conditions is met then the page is in focus.

How to get focus in jQuery?

The focus event occurs when an element gains focus.

When an element is selected by mouse click or positioned by tab key, the element will gain focus.

The focus() method triggers the focus event, or specifies a function to run when the focus event occurs.

Syntax

//触发 focus 事件
$(selector).focus()

//将函数绑定到 focus 事件
$(selector).focus(function)

function: Optional. Specifies the function to be run when the focus event occurs.

Example: focus() method to get focus

$("input").focus(function(){
  $("input").css("background-color","#FFFFCC");
});
<input type="text" focus="show();"/>

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of How to get 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