有没有一种方法可以将命令绑定到fadeIn事件而不必.trigger(' fadeIn')? [英] Is there a way to bind a command to fadeIn event without having to .trigger('fadeIn')?
问题描述
我有一个交互式表单系统,该表单系统具有不同的模块,其中每个模块的表单都包含在不同的div元素内.当我从一个模块更改为另一个模块时,我经常这样做:
I have an interactive form system that has different modules, where the forms for each module is contained inside a different div element. When i change from one module to another, I often do this:
$('#module_1').hide();
$('#module_2').fadeIn();
我喜欢这样,但是现在,在开发了一段时间该系统之后,我希望每次fadeIn
发生时都需要执行一些功能(例如重新初始化jqgrid).我是这样设置的:
I like this, but now, after developing this system for some time, I have some functions (like re-initialize a jqgrid) that I want to happen every time a fadeIn
occurs. I set up this up like this:
$('#module_2').bind('fadeIn',function(){
initialize_jqgrid();
});
是否有一种方法可以使$('#module_2').fadeIn();
的所有实例都可以使用此方法,而不必转到每个实例并将其替换为该实例?
Is there a way I can make this work for all instances of $('#module_2').fadeIn();
without having to go to every instance and replace it with this?
$('#module_2').fadeIn().trigger('fadeIn');
其背后的动机仅仅是拥有更简洁的代码,$('#module_2').fadeIn().trigger('fadeIn');
有点多余.
The motivation behind this is just to have cleaner code, $('#module_2').fadeIn().trigger('fadeIn');
is a little redundant.
非常感谢!
推荐答案
当然
var _old = $.fn.fadeIn;
$.fn.fadeIn = function(){
var self = this;
return _old.apply(this,arguments).promise().done(function(){
self.trigger("fadeIn");
});
};
// and your code: // replace .bind with .on if jQuery 1.7+
$("div").bind("fadeIn",function(){alert("worky");});
$("div").fadeIn(2000);
演示
http://jsfiddle.net/gEVsX/2/
更新评论
var _old = $.fn.fadeIn;
$.fn.fadeIn = function(){
return _old.apply(this,arguments).trigger("fadeIn");
};
// and your code: // replace .bind with .on if jQuery 1.7+
$("div").bind("fadeIn",function(){alert("worky");});
$("div").fadeIn(2000);
这篇关于有没有一种方法可以将命令绑定到fadeIn事件而不必.trigger(' fadeIn')?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!