我如何知道哪个元素从js中触发事件? [英] How can I know which element fire the event from js?

查看:484
本文介绍了我如何知道哪个元素从js中触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的事情:

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

我希望EventHandler监听双击事件,我希望EventHandler知道页面中的哪个元素是火这个事件,我怎么能这样做?谢谢。

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? 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
});

所以在处理程序中, e.target 可能是处理程序或子项(从中冒泡)的元素,以及将引用处理程序所在的元素,在这种情况下 #eventFire

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.

这篇关于我如何知道哪个元素从js中触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆