hook in jquery for toggle
I created a page contains the followi开发者_如何学Gong code. There I need to alert a message. But it does not work. WHY ?
Code:
$(document).ready(function() {
$.hook('toggle');
$('#test').bind('onbeforetoggle', function(e){alert(e.type)});
$('#btnHide').click(function() {
$('#divTest').toggle();
});
});
jsFiddle demo
hope some helps
http://jsfiddle.net/TzCXC/
(function($){
$.hook = function (fns) {
fns = typeof fns === 'string' ?
fns.split(' ') :
$.makeArray(fns)
;
jQuery.each( fns, function (i, method) {
var old = $.fn[ method ];
if ( old && !old.__hookold ) {
$.fn[ method ] = function () {
this.triggerHandler('onbefore'+method);
this.triggerHandler('on'+method);
var ret = old.apply(this, arguments);
this.triggerHandler('onafter'+method);
return ret;
};
$.fn[ method ].__hookold = old;
}
});
};
$.unhook = function (fns) {
fns = typeof fns === 'string' ?
fns.split(' ') :
$.makeArray(fns)
;
jQuery.each( $.makeArray(fns), function (i, method) {
var cur = $.fn[ method ];
if ( cur && cur.__hookold ) {
$.fn[ method ] = cur.__hookold;
}
});
};
})(jQuery);
精彩评论