Home > Web Front-end > JS Tutorial > body text

jQuery: Detailed explanation of the use of focus event

黄舟
Release: 2017-06-27 11:30:11
Original
1817 people have browsed it

This article mainly introduces the usage of focus events in jQuery, and analyzes the usage techniques of focus events when obtaining focus in the form of examples. It has certain reference value. Friends in need can refer to the examples of this article

Describes the usage of focus event in jQuery. Share it with everyone for your reference. The specific analysis is as follows:

The focus event is triggered when an element obtains focus or calls the focus() method.

A complete event process must not only have conditions that can trigger the event, but also have an event processing program.
You can bind an event handler to the focus event through the focus() method. For example:

$("input").focus(function(){$(this).css("backgroundColor","red")})
Copy after login

The above code is to bind functionfunction as an event handler to the focus event through the focus() method. This function will be called when the focus event is triggered.
Example code:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=" utf-8"> 
<meta name="author" content="http://www.jb51.net/" /> 
<title>focus事件-脚本之家</title> 
<script type="text/
javascript
" src="mytest/jQuery/jquery-1.8.3.js"></script> 
<script type="text/javascript"> 
$(
document
).ready(function(){ 
  $("input").focus(function(){
    $(this).css("backgroundColor","red")
  }) 
})  
</script> 
</head> 
<body> 
<p> 
<ul> 
  <li>用户名:<input type="text" name="username" /></li> 
  <li>密码:<input type="password" name="userpassword" /></li> 
</ul> 
</p> 
</body> 
</html>
Copy after login

The above code can register the focus event handler for the input element, and set the background color of the text box when it gets focus.

The above is the detailed content of jQuery: Detailed explanation of the use of focus event. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!