Home > Common Problem > body text

What does this refer to when binding click events in jquery?

尊渡假赌尊渡假赌尊渡假赌
Release: 2023-05-30 10:39:07
Original
1950 people have browsed it

This of jquery binding click event refers to the DOM object that triggers this event. The method is: 1. Create an html sample file and reference the jQuery file in the head; 2. Create a div tag in the body , and set the id to a; 3. Create a click event in the script and bind it to the div tag; 4. Click the div tag, the console prints "this", and the result is the "div" DOM ​​object that triggered the event.

What does this refer to when binding click events in jquery?

The operating system of this tutorial: Windows 10 system, jQuery3.6.0 version, Dell G3 computer.

The this of jquery binding click event refers to the DOM object that triggered this event.

1. Create an html sample file and reference the jQuery file in the head

<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
    <script src="../scripts/jquery.js" type="text/javascript"></script>
</head>
</html>
Copy after login

2. Create a div tag in the body and set the id to a

<body>
<div id="a">clickme</div>
</body>
Copy after login

3. Create a click event in the script and bind it to the div

$("#a").click(function(){
  console.log(this) 
});
Copy after login

4. Click the div tag, and the console prints out the result as the DOM object that triggered the event

屏幕截图 2023-05-30 102835.png

The above is the detailed content of What does this refer to when binding click events in jquery?. 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