You could always add to the original .show() method so you don't have to trigger events every time you show something or if you need it to work with legacy code:(您始终可以添加到原始.show()方法,这样您就不必在每次显示某些内容时触发事件,或者如果需要它来处理遗留代码:)
Jquery extension:(Jquery扩展:)
jQuery(function($) {
var _oldShow = $.fn.show;
$.fn.show = function(speed, oldCallback) {
return $(this).each(function() {
var obj = $(this),
newCallback = function() {
if ($.isFunction(oldCallback)) {
oldCallback.apply(obj);
}
obj.trigger('afterShow');
};
// you can trigger a before show if you want
obj.trigger('beforeShow');
// now use the old function to show the element passing the new callback
_oldShow.apply(obj, [speed, newCallback]);
});
}
});
Usage example:(用法示例:)
jQuery(function($) {
$('#test')
.bind('beforeShow', function() {
alert('beforeShow');
})
.bind('afterShow', function() {
alert('afterShow');
})
.show(1000, function() {
alert('in show callback');
})
.show();
});
This effectively lets you do something beforeShow and afterShow while still executing the normal behavior of the original .show() method.(这有效地允许您在beforeShow和afterShow之前执行某些操作,同时仍然执行原始.show()方法的正常行为。)
You could also create another method so you don't have to override the original .show() method.(您还可以创建另一个方法,这样就不必重写原始的.show()方法。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…