[英]How do you get the source element which fired an event when using an object method
[英]How to get the object which fired the event in jquery?
在javascript中,我們具有event.srcElement
,它提供了發生事件的元素。 有什么方法可以在jQuery中獲得此對象。
也,
function myFun(){
alert ( $(this) ); // what do this object represent? Is it what i need ?
}
簡短的回答:如果您在事件處理程序內部,則可以, this
是您大部分時間所關心的。
在事件處理程序內部, this
指的是您要執行的操作或在需要時訪問傳入的事件對象 ,例如:
$(".element").click(function(e) {
//this is the element clicked
//e.target is the target of the event, could be a child element
});
例如,如果您的click
處理程序實際上是在父級上,並且您單擊了其中的某個內容,則本例中的錨點為:
<div id="parent"><a href="#">Test</a></div>
使用此處理程序:
$("#parent").click(function(e) {
//this is the <div id="parent"> element
//e.target is the <a> element
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.