jQuery中的event.currenttarget属性用于在事件冒泡阶段返回当前DOM元素。
event-currentTarget通常等于" this "。
语法如下:
event.currentTarget
参数:
- 事件:它是必填参数, 该参数来自事件绑定函数。
示例1:
<!DOCTYPE html>
<html>
<head>
<title>
jQuery event.currentTarget Property
</title>
<script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function() {
$("h1, h2, p").click(function(event) {
alert(event.currentTarget === this);
});
});
</script>
</head>
<body>
<center>
<h1 id = "geeks1" style = "color:green;">
lsbin
</h1>
<h2 id = "geeks2">
jQuery event.currentTarget Property
</h2>
<p>
<b>Note:</b> Click on each HTML element.
</p>
</center>
</body>
</html>
输出如下:
点击前:
单击任何元素后:
示例2:
<!DOCTYPE html>
<html>
<head>
<title>
jQuery event.currentTarget Property
</title>
<script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function() {
$("h1, p").click(function(event) {
alert(event.currentTarget.innerHTML);
});
});
</script>
</head>
<body>
<center>
<h1 id = "geeks1"
style = "color:green;">
lsbin
</h1>
<h2 id = "geeks2">
jQuery event.currentTarget Property
</h2>
<p>
<b>Note:</b> Click on each HTML element.
</p>
</center>
</body>
</html>
输出如下:
点击前:
单击lsbin之后: