$.extend({
pluginName = function(){
//Plugin code here
};
})
})(jQuery, window, document);
调用方法:$.pluginName();
2、对象级别的插件开发
对象级别插件写法:
//方式1
;(function($, window, document, undefined){
$.fn.pluginName = function(options) {
return this.each(function() {
//this关键字代表了这个插件将要执行的jQuery对象
//return this.each()使得插件能够形成链式调用
var defaults = {
//pro : value
};
var settings = $.extend({}, defaults, options);
// plugin implementationcode here
});
}
})(jQuery, window, document);
//方式2
;(function($, window, document, undefined){
$.fn.extend({
pluginName : function(){
return this.each(function(){
// plugin code here
});
};
})
})(jQuery, window, document);
//方式3 这种类型的插件架构允许您封装所有的方法在父包中,通过传递该方法的字符串名称和额外的此方法需要的参数来调用它们。
;(function($, window, document, undefined){
// 在我们插件容器内,创造一个公共变量来构建一个私有方法
var privateFunction = function() {
// code here
}
// 通过字面量创造一个对象,存储我们需要的公有方法
var methods = {
// 在字面量对象中定义每个单独的方法
init: function() {
// 为了更好的灵活性,对来自主函数,并进入每个方法中的选择器其中的每个单独的元素都执行代码
return this.each(function() {
// 为每个独立的元素创建一个jQuery对象
var $this = $(this);
// 创建一个默认设置对象
var defaults = {
propertyName: 'value',
onSomeEvent: function() {}
} // 使用extend方法从options和defaults对象中构造出一个settings对象
var settings = $.extend({}, defaults, options);
// 执行代码
// 例如: privateFunction();
});
},
destroy: function() {
// 对选择器每个元素都执行方法
return this.each(function() {
// 执行代码
});
}
};
$.fn.pluginName = function() {
// 获取我们的方法,遗憾的是,如果我们用function(method){}来实现,这样会毁掉一切的
var method = arguments[0];
// 检验方法是否存在
if(methods[method]) {
// 如果方法存在,存储起来以便使用
// 注意:我这样做是为了等下更方便地使用each()
method = methods[method];
// 如果方法不存在,检验对象是否为一个对象(JSON对象)或者method方法没有被传入
} else if( typeof(method) == 'object' || !method ) {










