简体   繁体   English

我如何知道哪个元素从js中触发事件?

[英]How can I know which element fire the event from js?

I have somethings like this: 我有这样的事情:

$('#eventFire').dblclick(function(){ 
         EventHandler.dblclickListener();
});   

I want the EventHandler listen the double click event, and I want the EventHandler know which element from the page is fire this event, how can I do so? 我希望EventHandler监听双击事件,我希望EventHandler知道页面中的哪个元素触发了这个事件,我该怎么办呢? Thank you. 谢谢。

The event object is passed as the first argument to your handler, like this: 事件对象作为第一个参数传递给您的处理程序,如下所示:

$('#eventFire').dblclick(function(e){ 
  //e.target fired the event, this refers to the #eventFire element
});

So inside the handler, the e.target could be the element with the handler or a child (from which the even bubbled), and this will refer to the element the handler is on, #eventFire in this case. 因此,在处理程序中, e.target可以是具有处理程序或子项的元素(甚至起泡), this将引用处理程序所在的元素,在这种情况下为#eventFire

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM