(function($) {
/**
*zw*/
$.fn.baseClick = function(optionsData) {
$(this).on("click", function(){
var defaults = {//这个是定义baseClick内部方法的类 可修改
init : function(options) { alert("init options" +options); console.log(options);},
show : function() {alert("show " ); },
hide : function() {alert("show " ); },
query : function() { alert("hide " ); },
back : function() { alert("hide " ); },
remove : function() { alert("hide " ); },
update : function(options) { alert("update content" +options); console.log(options);},
add : function(options) { alert("add content" +options); console.log(options);}
};
// var _Self = this,$this = $(this);
this._Config = $.extend(defaults, optionsData);
var args = arguments;
// 调用方法
if ( this._Config[optionsData] ) {
//data就是传过来的参数 获取第0个参数参数
args[0]=optionsData;
var data=Array.prototype.slice.call(arguments, 0);
//把method的内容,以字符串的形式作为参数传入
return this._Config[ optionsData ].apply( this,data);
} else if ( typeof optionsData === "object" || ! optionsData ) {
var method= optionsData["method"];
args[0]=optionsData;
return this._Config[method].apply(this,args );
} else {
console.log( "Method " + optionsData + " does not exist on jQuery.Plugin" );
}
});
};
})(jQuery);
调用:
var dat={method:"init",init:function(options){alert(123+options); console.log(options);}};
$("#test").baseClick(dat);
$("#test1").baseClick("init");