Home  >  Article  >  Web Front-end  >  How to determine whether focus has been lost in jquery

How to determine whether focus has been lost in jquery

WBOY
WBOYOriginal
2022-04-02 12:05:332634browse

In jquery, the blur() and focus() methods can be used to determine whether an element has lost focus. It is used to trigger events when an element loses and gains focus. The syntax is "element object.blur(function(){lost focus" Code;}Element object.focus(function(){Get focus code;}".

How to determine whether focus has been lost in jquery

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

How does jquery determine whether it has lost focus

The blur event occurs when an element loses focus.

The blur() method triggers the blur event , or specify a function to be run when a blur event occurs.

Trigger the blur event for the selected element:

$(selector).blur()

Add a function to the blur event:

$(selector).blur(function)

When When an element gains focus, a focus event occurs.

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

Triggers the focus event of the selected element :

$(selector).focus()

Add function to focus event:

$(selector).focus(function)

The example is as follows:

<html>
<head>
<meta charset="utf-8"> 
<title>123</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("input").blur(function(){
    document.getElementById(&#39;city&#39;).innerHTML="失去焦点了";
  });
  $("input").focus(function(){
    document.getElementById(&#39;city&#39;).innerHTML="获得焦点了";
  });
});
</script>
</head>
<body>
输入你的名字: <input type="text">
<p id="city">判断输入框是否失去焦点</p>
</body>
</html>

Output result:

How to determine whether focus has been lost in jquery

Recommended related video tutorials: jQuery video tutorial

The above is the detailed content of How to determine whether focus has been lost 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