(function ($) {
$.fn.greenify = function(options) {
// This is the easiest way to have default options.
var settings = $.extend({
// These are the defaults.
color: "#556b2f",
backgroundColor: "white"
}, options);
// Greenify the collection based on the settings variable.
return this.css({
color: settings.color,
backgroundColor: settings.backgroundColor
});
};
}(jQuery));
取自Docs上面的代碼展示瞭如何製作一個簡單的jquery插件。如果我只提供接受多個插件的多個參數中的一個,該怎麼辦?哪些是適用的?
如果我有一個插件,可以採用多個參數,哪些會在這樣的應用:$.doStuff({color:"black"},{color:"blue"});
什麼選項集將被「擴展」,如果只提供了一組參數的時候?
E.x. $.doStuff({color:"black"});
如何在jquery插件中包含可選參數
好吧,通常你不會有多個參數,相反你會有一個具有多個屬性的對象。 –