作者 Moshe Kolodny, 12月27日,,2013
;(function($) { // multiple plugins can go here (function(pluginName) { var defaults = { color: 'black', testFor: function(div) { return true; } }; $.fn[pluginName] = function(options) { options = $.extend(true, {}, defaults, options); return this.each(function() { var elem = this, $elem = $(elem); // heres the guts of the plugin if (options.testFor(elem)) { $elem.css({ borderWidth: 1, borderStyle: 'solid', borderColor: options.color }); } }); }; $.fn[pluginName].defaults = defaults; })('borderize'); })(jQuery);
$('.borderize').borderize({ testFor: function(elem) { var $elem = $(elem); if (elem.is('.inactive')) { return false; } else { // calling "parent" function return $.fn.borderize.defaults.testFor.apply(this, arguments); } } }); We can even do this with regular properties like this var someVarThatMayBeSet = false; /* code ... */ $('.borderize').borderize({ color: someVarThatMayBeSet ? 'red' : $.fn.borderize.defaults.color });
原文地址: kolodny.
|
|
來自: ThinkTank_引擎 > 《命名空間》