(function ( $ ) {
$.fn.greenify = function( options ) {
var settings = $.extend({
color: "#556b2f"
}, options );
return this.css({color: settings.color});
};
//TODO another function here
if ( action === "open") {
alert("Open!");
}
if ( action === "close" ) {
alert("lose!");
}
}( jQuery ));
`$( "div" ).greenify({color: "orange"});`//setting of options
我理解上面的代码设置greenify插件的选项,但我也想调用显示功能。
如何在设置仍可用于绿色化的选项时实现此类用法:
`$( "div" ).greenify("open");`
和
$( "div" ).greenify(open,{color: "orange"});
答案 0 :(得分:1)
您只需检查options
是对象还是字符串
$.fn.greenify = function(options) {
if (typeof options === 'string') {
if (options === 'open') {
alert('open');
} else if (options === 'close') {
alert('close');
}
} else {
var settings = $.extend({
color: "#556b2f"
}, options);
return this.css({
color: settings.color
});
}
}