To obtain focus, you need to use jQuery's focus method.
Definition and Usage
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.
Bind a function to the focus event
Syntax
$(selector).focus(function)
function: Optional. Specifies the function to be run when the focus event occurs.
Example:
Lock focus
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("input").focus(function(){ $("input").css("background-color","#FFFFCC"); }); }); </script> </head> <body> Enter your name: <input type="text" /> <p>请在上面的输入域中点击,使其获得焦点,然后就锁定焦点了。</p> </body> </html>
The above is the detailed content of How to lock focus in jquery. For more information, please follow other related articles on the PHP Chinese website!