Event delegation plugin
Event delegation plugin
Hi,
because of the bad performance of the .live-event, I wrote a little plugin to handle event delegation.
Now there are several plugins out there, but they are much bigger and slower than the following one.
So I ask myself: Did I miss something?
The plugin:
-
// listen
$.fn.listen = function(event, selector, callback) {
return this.bind(event, function(e){
el = $(e.target).closest(selector, this);
if (el.length) callback.call(this, e, el);
});
}
Examples:
-
$('#home').listen('click', 'a', function() { alert('wooord!') });
-
$('#home').listen('click', 'a', function(e, el) { el.hide() });
Greets,
McSod