热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

我能找到用jQuery绑定到元素上的事件吗?-CanIfindeventsboundonanelementwithjQuery?

Ibindtwoeventhandlersonthislink:我在这个链接上绑定了两个事件处理程序:<ahref#>ShowAlert&l

I bind two event handlers on this link:

我在这个链接上绑定了两个事件处理程序:

Show Alert

Javascript:

Javascript:

$(function()
{
  $('#elm').click(_f);
  $('#elm').mouseover(_m);
});

function _f(){alert('clicked');}
function _m(){alert('mouse over');}

Is there any way to get a list of all events bound on an element, in this case on element with id="elm"?

是否有方法获得绑定在元素上的所有事件的列表,在本例中是id="elm"的元素?

8 个解决方案

#1


462  

In modern versions of jQuery, you would use the $._data method to find any events attached by jQuery to the element in question. Note, this is an internal-use only method:

在现代jQuery版本中,您将使用$。_data方法,以查找由jQuery附加到该元素的任何事件。注意,这是一个内部使用的方法:

// Bind up a couple of event handlers
$("#foo").on({
    click: function(){ alert("Hello") },
    mouseout: function(){ alert("World") }
});

// Lookup events for this particular Element
$._data( $("#foo")[0], "events" );

The result from $._data will be an object that contains both of the events we set (pictured below with the mouseout property expanded):

美元的结果。_data将是一个包含我们设置的事件的对象(如下图所示的mouseout属性扩展):

Console output for $._

Then in Chrome, you may right click the handler function and click "view function definition" to show you the exact spot where it is defined in your code.

然后,在Chrome中,您可以右键单击处理程序函数并单击“视图函数定义”,以显示在代码中定义它的确切位置。

#2


59  

General case:

一般情况下:

  • Hit F12 to open Dev Tools
  • 按F12打开开发工具
  • Click the Sources tab
  • 单击“源”选项卡
  • On right-hand side, scroll down to Event Listener Breakpoints, and expand tree
  • 在右边,向下滚动到事件侦听器断点,并展开树
  • Click on the events you want to listen for.
  • 单击要侦听的事件。
  • Interact with the target element, if they fire you will get a break point in the debugger
  • 与目标元素交互,如果它们开火,您将在调试器中获得一个断点。

Similarly, you can:

类似地,您可以:

  • right click on the target element -> select "Inspect element"
  • 右键单击目标元素——>选择“Inspect element”
  • Scroll down on the right side of the dev frame, at the bottom is 'event listeners'.
  • 在dev框架的右侧向下滚动,底部是“事件监听器”。
  • Expand the tree to see what events are attached to the element. Not sure if this works for events that are handled through bubbling (I'm guessing not)
  • 展开该树,查看该元素上附加了哪些事件。不确定这是否适用于通过冒泡处理的事件(我猜不是)

#3


11  

I'm adding this for posterity; There's an easier way that doesn't involve writing more JS. Using the amazing firebug addon for firefox,

我把这个加给后代;有一种更简单的方法不需要编写更多的JS。使用令人惊叹的firebug插件为firefox,

  1. Right click on the element and select 'Inspect element with Firebug'
  2. 右键单击元素并选择“使用Firebug检查元素”
  3. In the sidebar panels (shown in the screenshot), navigate to the events tab using the tiny > arrow
  4. 在工具条面板(如屏幕截图所示)中,使用小的>箭头导航到events选项卡
  5. The events tab shows the events and corresponding functions for each event
  6. events选项卡显示每个事件的事件和相应的函数。
  7. The text next to it shows the function location
  8. 它旁边的文本显示函数位置

enter image description here

#4


6  

The jQuery Audit plugin plugin should let you do this through the normal Chrome Dev Tools. It's not perfect, but it should let you see the actual handler bound to the element/event and not just the generic jQuery handler.

jQuery审计插件插件应该允许您通过普通的Chrome开发工具进行审计。它并不完美,但它应该让您看到绑定到元素/事件的实际处理程序,而不仅仅是一般的jQuery处理程序。

#5


2  

While this isn't exactly specific to jQuery selectors/objects, in FireFox Quantum 58.x, you can find event handlers on an element using the Dev tools:

虽然这并不是针对jQuery选择器/对象的,但是在FireFox Quantum 58中。您可以使用Dev工具在元素上找到事件处理程序:

  1. Right-click the element
  2. 右键单击元素
  3. In the context menu, Click 'Inspect Element'
  4. 在上下文菜单中,单击“查看元素”
  5. If there is an 'ev' icon next to the element (yellow box), click on 'ev' icon
  6. 如果在元素(黄色框)旁边有一个“ev”图标,点击“ev”图标。
  7. Displays all events for that element and event handler
  8. 显示该元素和事件处理程序的所有事件

FF Quantum Dev Tools

#6


1  

I used something like this if($._data($("a.wine-item-link")[0]).events == null) { ... do something, pretty much bind their event handlers again } to check if my element is bound to any event. It will still say undefined (null) if you have unattached all your event handlers from that element. That is the reason why I am evaluating this in an if expression.

我使用了这样的if($._data($(“a.wine-item-link”)[0]))。事件== null){…做点什么,再次绑定事件处理程序}来检查我的元素是否绑定到任何事件。如果您从该元素中删除了所有事件处理程序,它仍然会说未定义(null)。这就是我用if表达式求值的原因。

#7


0  

When I pass a little complex DOM query to $._data like this: $._data($('#outerWrap .innerWrap ul li:last a'), 'events') it throws undefined in the browser console.

当我将一个复杂的DOM查询传递给$时。_data:美元。_data($('#outerWrap . innerwrap ul li:last a'), 'events')它在浏览器控制台中未定义地抛出。

So I had to use $._data on the parent div: $._data($('#outerWrap')[0], 'events') to see the events for the a tags. Here is a JSFiddle for the same: http://jsfiddle.net/giri_jeedigunta/MLcpT/4/

所以我必须用$。父div的数据:$._data($('#outerWrap')[0], 'events')来查看标记的事件。这里有一个JSFiddle: http://jsfiddle.net/giri_jeedigunta/MLcpT/4/

#8


0  

Note that events may be attached to the document itself rather than the element in question. In that case, you'll want to use:

注意,事件可以附加到文档本身,而不是相关的元素。在这种情况下,您需要使用:

$._data( $(document)[0], "events" );

And find the event with the correct selector:

用正确的选择器找到事件:

enter image description here

And then look at the handler > [[FunctionLocation]]

然后看看处理器> [[FunctionLocation]]

enter image description here


推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文介绍了Oracle存储过程的基本语法和写法示例,同时还介绍了已命名的系统异常的产生原因。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • 本文介绍了在满足特定条件时如何在输入字段中使用默认值的方法和相应的代码。当输入字段填充100或更多的金额时,使用50作为默认值;当输入字段填充有-20或更多(负数)时,使用-10作为默认值。文章还提供了相关的JavaScript和Jquery代码,用于动态地根据条件使用默认值。 ... [详细]
author-avatar
卖火柴的萌小猪_966
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有