Home >Common Problem >What does this refer to when binding click events in jquery?
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.
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>
2. Create a div tag in the body and set the id to a
<body> <div id="a">clickme</div> </body>
3. Create a click event in the script and bind it to the div
$("#a").click(function(){ console.log(this) });
4. Click the div tag, and the console prints out the result as the DOM object that triggered the event
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!