jQuery 的 event.currentTarget 属性用于返回事件冒泡阶段中的当前 DOM 元素. 该 event.currentTarget 通常等于 "this".
语法:
event.currentTarget
参数:
event: 它是必需参数, 该参数来自事件绑定函数.
示例:
- <!DOCTYPE html>
- <HTML>
- <head>
- <meta charset="UTF-8">
- <script src="https://code.jquery.com/jquery-3.3.1.min.js">
- </script>
- <script>
- $(document).ready(function() {
- $("h2, p").click(function(event) {
- alert(event.currentTarget === this);
- });
- });
- </script>
- </head>
- <body>
- <center>
- <h2 id="geeks2">
- jQuery event.currentTarget 属性
- </h2>
- <p>
- 单击显示的 HTML 元素
- </p>
- </center>
- </body>
- </HTML>
因为 currentTarget 类似于 "this", 所以返回 "true".
输出:
示例 2:
- <!DOCTYPE HTML>
- <HTML>
- <head>
- <meta charset="UTF-8">
- <script src="https://code.jquery.com/jquery-3.3.1.min.js">
- </script>
- <script>
- $(document).ready(function() {
- $("h2,p").click(function(event) {
- alert(event.currentTarget.innerHTML);
- });
- });
- </script>
- </head>
- <body>
- <center>
- <h2 id="geeks2">
- Hello World!
- </h2>
- <p>
- 单击每个 HTML 元素
- </p>
- </center>
- </body>
- </HTML>
单击 h2 元素后, 输出:
单击 p 元素后, 输出:
来源: http://www.css88.com/qa/jquery/10463.html