检查元素上是否存在事件 [英] Check if event exists on element

查看:150
本文介绍了检查元素上是否存在事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检查jQuery中是否存在事件?我正在开发一个使用自定义命名空间事件的插件,并且希望能够检查事件是否绑定到元素。

Is there a way to check if an event exists in jQuery? I’m working on a plugin that uses custom namespaced events, and would like to be able to check if the event is binded to an element or not.

推荐答案

$('body').click(function(){ alert('test' )})

var foo = $.data( $('body').get(0), 'events' ).click
// you can query $.data( object, 'events' ) and get an object back, then see what events are attached to it.

$.each( foo, function(i,o) {
    alert(i) // guid of the event
    alert(o) // the function definition of the event handler
});

您可以通过将对象引用(不是jQuery对象)提供给$ .data进行检查,并且对于第二个参数feed'events',它将返回一个填充了所有事件的对象,例如'click'。您可以遍历该对象并查看事件处理程序的作用。

You can inspect by feeding the object reference ( not the jQuery object though ) to $.data, and for the second argument feed 'events' and that will return an object populated with all the events such as 'click'. You can loop through that object and see what the event handler does.

这篇关于检查元素上是否存在事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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