[英]Event.target refer to the child, not the parent
考慮以下嵌套div
<div class="parent">
<div class="child"></div>
</div>
在jQuery中,我做到了
$('.parent').click(function (e) {
e.target // this refers to .child instead of .parent
})
有沒有辦法在這種情況下獲得對.parent
的引用, .parent
不是像.closest()
?
您可以使用$(this)
,而不是e.target
和this
,你可能想使用它的jQuery方法。
$('.parent').click(function(e) { alert($(this).attr('class')); });
.parent { color: green; } .child { color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <div class="parent"> Parent <div class="child">Clild</div> </div>
$(this)
將始終引用發生事件的元素。
我很困惑,為什么我不能簡單地使用$(this)
引用。 我使用Meteor
結合this
在事件回調到一個不同的對象。 在這種情況下,您可以使用event.currentTarget
,我相信jQuery如何綁定this
引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.