[英]jQuery - Callback failing if there is no options parameter
我正在嘗試構建一個像這樣的簡單插件
(function($) {
$.fn.gpSlideOut = function(options, callback) {
// default options - these are used when no others are specified
$.fn.gpSlideOut.defaults = {
fadeToColour: "#ffffb3",
fadeToColourSpeed: 500,
slideUpSpeed: 400
};
// build main options before element iteration
var o = $.extend({}, $.fn.gpSlideOut.defaults, options);
this.each(function() {
$(this)
.animate({backgroundColor: o.fadeToColour},o.fadeToColourSpeed)
.slideUp(o.SlideUpSpeed, function(){
if (typeof callback == 'function') { // make sure the callback is a function
callback.call(this); // brings the scope to the callback
}
});
});
return this;
};
// invoke the function we just created passing it the jQuery object
})(jQuery);
我一直感到困惑的是,通常在jQuery插件上,您可以這樣調用:
$(this_moveable_item).gpSlideOut(function() {
// Do stuff
});
沒有options參數,但是如果我這樣做,它將錯過回調,所以我必須始終
var options = {}
$(this_moveable_item).gpSlideOut(options, function() {
// Do stuff
});
即使我只想使用默認值。
無論如何,無論是否有options參數,都要確保調用了回調函數?
干杯。
//you have no second parameter
if (callback == undefined)
...
//have a function
if ($.isFunction(options))
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.