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

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

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2023-05-30 10:32:011931browse

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>

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

屏幕截图 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!

Statement:
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