我在創建回調函數時遇到了問題。首先,據我所知,回調就像一個參數,當其「父」函數中的所有動作完成時觸發。例如:Jquery回調函數
$('p').hide('slow', function() {
$(this).show('slow');
});
起初<p>
被隱藏起來,只有事後再次顯示。
如何將這種回調應用於隨機插件中?
例如一個插件看起來是這樣的:
$.fn.myPlugin = function(settings) {
return this.each(function() {
settings = $.extend({ onShow: null }, settings);
$(this).hide('slow'); /*This action should happen first (First action)*/
if ($.isFunction(settings.onShow))
settings.onShow.call(this);/*Callback should fire after First action has stopped*/
}
});
};
$(document).ready(function()
$('p').myPlugin({
onShow: function() {
alert('My callback!');/*Both actions(element hiding and alert)
fire simultaneously, but I need alert
to fire after element is hidden*/
}
});
});
你究竟想完成什麼? – quantumSoup 2010-06-29 12:49:24
我正在嘗試回調以正確的方式工作,就像在大多數jQuery方法中一樣。 $(document).ready(function(){*在這裏發生的Everething發生在Dom被加載之後,但之前沒有,所以它是一個回調* /});我想知道如何在我的插件中寫回調 – tylik 2010-06-29 16:12:15