How can I check all of the bindings for an element with jQuery?
I have some elements and I'm using some jQuery plugins. I want to see if the elements are already bound 开发者_开发技巧to events.
Using firebug:
console.log( jQuery(someElements).data('events') );
Note that this will only return events bound by jQuery's event mechanism.
The returned object will be in the following format (note this is assuming 1.4):
{
eventName /* e.g. "click" */: [/* handler array */
{
/* handler object */
data: /* data passed to handler */,
guid: /* guid, for internal use */,
namespace: /* for namespaced events */,
type: /* event name, e.g. "click" */,
handler: /* actual handler function */
}
]
}
If firefox/firebug is your js debugging environment you can add http://www.softwareishard.com/blog/firebug/eventbug-alpha-released/ to firebug
Here's another note: jQuery lets you bind events with a 2-dimensional namespace. If you want to be able to bind and unbind your event handlers without disturbing other stuff, you can use a dotted-pair notation to give your own "click" handler an identity that'll let you later unbind it without screwing up unrelated code:
$('#something').bind('click.myClick', function() { ... });
then later:
$('#something').unbind('click.myClick');
精彩评论