List all javascript events wired up on a page using jquery

pyoracle發表於2011-04-20

(function($) {
$.eventReport = function(selector, root) {
var s = [];
$(selector || '*', root).andSelf().each(function() {
var e = $.data(this, 'events');
if(!e) return;
s.push(this.tagName);
if(this.id) s.push('#', this.id);
if(this.className) s.push('.', this.className);
for(var p in e) s.push('n', p);
s.push('nn');
});
return s.join('');
}
$.fn.eventReport = function(selector) {
return $.eventReport(selector, this);
}
})(jQuery);

[@more@]
// all events
alert
($.eventReport());// just events on inputs
alert
($.eventReport('input')); // just events assigned to this element
alert
($.eventReport('#myelement')); // events assigned to inputs in this element
alert
($.eventReport('input', '#myelement'));
alert
($('#myelement').eventReport('input')); // same result// just events assigned to this element's children
alert
($('#myelement').eventReport());
alert
($.eventReport('*', '#myelement'); // same result

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25556693/viewspace-1048918/,如需轉載,請註明出處,否則將追究法律責任。

相關文章