次のプラグインを考えると、すべてのインスタンスのデフォルトをどのように設定しますか? $ .datepicker.setDefaults()と同じように動作させたいのですが。
(function($) {
$.fn.borderSwitcher = function(options) {
defaults = {
borderColor: "Black",
borderWidth: "1px",
borderStyle: "solid"
};
return this.each(function() {
var settings = $.extend(defaults, options);
$(this).focus(function() {
//find a better way to set border properties
var props = settings.borderStyle + " " + settings.borderWidth + " " + settings.borderColor;
$(this).css("border", props);
});
$(this).blur(function() {
$(this).css("border", "");
});
});
};
})(jQuery);
回答:
回答№1は1それらをjQueryオブジェクト自体に追加します。
(function ($) {
$.borderSwitcher.defaults = { //NOTE: no fn here
borderColor: "Black",
borderWidth: "1px",
borderStyle: "solid"
};
$.fn.borderSwitcher = function (options) {
var settings = $.extend($.borderSwitcher.defaults, options);
//the rest of your code
}
};
//And outside your plugin definition, set the default for all new instances
$.borderSwitcher.defaults.borderWidth = "2px";
$(".foo").borderSwitcher(); //with 2px
$(".foo").borderSwitcher({borderWidth:"3px"}); //with 3px