插件开发两种方式
1:类级插件开发
/*
* jquery 类级插件开发
* 1:直接给jquery添加全局函数
* 2:用extend()方法
* 3:使用命名空间
* **/
//直接给jquery添加全局函数
jQuery.myAlert=function(objText){
alert(objText);
};
//引用
$.myAlert("直接给jquery添加全局函数");
//用extend()方法
jQuery.extend({
myAlert:function(objText){
alert(objText);
},
myAlert2:function(objText){
alert(objText);
}
});
//引用
$.myAlert("直接给jquery添加全局函数");
//使用命名空间
jQuery.zxit={
myAlert:function(objText){
alert(objText);
},
};
//引用
$.zxit.myAlert("直接给jquery添加全局函数");
2:对象级插件开发
;(function(){
$.fn.plugin = function(options){
var defaults = {
//各种参数,各种属性
}
var options = $.extend(defaults.options);
this.each(function(){
//实现功能代码
});
return this;
}
})(jQuery);