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;}".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
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('city').innerHTML="失去焦点了"; }); $("input").focus(function(){ document.getElementById('city').innerHTML="获得焦点了"; }); }); </script> </head> <body> 输入你的名字: <input type="text"> <p id="city">判断输入框是否失去焦点</p> </body> </html>
Output result:
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!