$.fn.pluginName = function() {
//your code here
}
var Haorooms= function(el, opt) {
this.$element = el,
this.defaults = {
'color': 'red',
'fontSize': '12px',
'textDecoration':'none'
},
this.options = $.extend({}, this.defaults, opt)
}
//定义haorooms的方法
haorooms.prototype = {
changecss: function() {
return this.$element.css({
'color': this.options.color,
'fontSize': this.options.fontSize,
'textDecoration': this.options.textDecoration
});
}
}
$.fn.myPlugin = function(options) {
//创建haorooms的实体
var haorooms= new Haorooms(this, options);
//调用其方法
return Haorooms.changecss();
}
$(function() {
$('a').myPlugin({
'color': '#2C9929',
'fontSize': '20px'
});
})
(function(){
})()
var haoroomsblog=function(){
}
(function(){
})()
;(function(){
})()
;(function(){
})()
;(function($,window,document,undefined){
//我们的代码。。
})(jQuery,window,document);
(function($) {
var privateFunction = function() {
// 代码在这里运行
}
var methods = {
init: function(options) {
return this.each(function() {
var $this = $(this);
var settings = $this.data('pluginName');
if(typeof(settings) == 'undefined') {
var defaults = {
propertyName: 'value',
onSomeEvent: function() {}
}
settings = $.extend({}, defaults, options);
$this.data('pluginName', settings);
} else {
settings = $.extend({}, settings, options);
}
// 代码在这里运行
});
},
destroy: function(options) {
return $(this).each(function() {
var $this = $(this);
$this.removeData('pluginName');
});
},
val: function(options) {
var someValue = this.eq(0).html();
return someValue;
}
};
$.fn.pluginName = function() {
var method = arguments[0];
if(methods[method]) {
method = methods[method];
arguments = Array.prototype.slice.call(arguments, 1);
} else if( typeof(method) == 'object' || !method ) {
method = methods.init;
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.pluginName' );
return this;
}
return method.apply(this, arguments);
}
})(jQuery);
(function(arg1, arg2) {
// 代码
})(arg1, arg2);
(function($) {
// 局部作用域中使用$来引用jQuery
})(jQuery);
(function($) {
$.fn.pluginName = function(options) {
// 代码在此处运行
return this;
}
})(jQuery);
function($) {
// 向jQuery中被保护的“fn”命名空间中添加你的插件代码,用“pluginName”作为插件的函数名称
$.fn.pluginName = function(options) {
// 返回“this”(函数each()的返回值也是this),以便进行链式调用。
return this.each(function() {
// 此处运行代码,可以通过“this”来获得每个单独的元素
// 例如: $(this).show();
var $this = $(this);
});
}
})(jQuery);
$('#element').pluginName('val');
// 会返回我们需要的值,而不是一个jQuery对象
(function($) {
// 在我们插件容器内,创造一个公共变量来构建一个私有方法
var privateFunction = function() {
// code here
}
// 通过字面量创造一个对象,存储我们需要的共有方法
var methods = {
// 在字面量对象中定义每个单独的方法
init: function() {
// 为了更好的灵活性,对来自主函数,并进入每个方法中的选择器其中的每个单独的元素都执行代码
return this.each(function() {
// 为每个独立的元素创建一个jQuery对象
var $this = $(this);
// 执行代码
// 例如: 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 ) {
// 如果我们传入的是一个对象参数,或者根本没有参数,init方法会被调用
method = methods.init;
} else {
// 如果方法不存在或者参数没传入,则报出错误。需要调用的方法没有被正确调用
$.error( 'Method ' + method + ' does not exist on jQuery.pluginName' );
return this;
}
// 调用我们选中的方法
// 再一次注意我们是如何将each()从这里转移到每个单独的方法上的
return method.call(this);
}
})(jQuery);
/*
注意这些例子可以在目前的插件代码中正确运行,并不是所有的插件都使用同样的代码结构
*/
// 为每个类名为 ".className" 的元素执行init方法
$('.className').pluginName();
$('.className').pluginName('init');
$('.className').pluginName('init', {}); // 向init方法传入“{}”对象作为函数参数
$('.className').pluginName({}); // 向init方法传入“{}”对象作为函数参数
// 为每个类名为 “.className” 的元素执行destroy方法
$('.className').pluginName('destroy');
$('.className').pluginName('destroy', {}); // 向destroy方法传入“{}”对象作为函数参数
// 所有代码都可以正常运行
$('.className').pluginName('init', 'argument1', 'argument2'); // 把 "argument 1" 和 "argument 2" 传入 "init"
// 不正确的使用
$('.className').pluginName('nonexistantMethod');
$('.className').pluginName('nonexistantMethod', {});
$('.className').pluginName('argument 1'); // 会尝试调用 "argument 1" 方法
$('.className').pluginName('argument 1', 'argument 2'); // 会尝试调用 "argument 1" ,“argument 2”方法
$('.className').pluginName('privateFunction'); // 'privateFunction' 不是一个方法
(function($) {
var methods = {
init: function(options) {
// 在每个元素上执行方法
return this.each(function() {
var $this = $(this);
// 创建一个默认设置对象
var defaults = {
propertyName: 'value',
onSomeEvent: function() {}
}
// 使用extend方法从options和defaults对象中构造出一个settings对象
var settings = $.extend({}, defaults, options);
// 执行代码
});
}
};
$.fn.pluginName = function() {
var method = arguments[0];
if(methods[method]) {
method = methods[method];
// 我们的方法是作为参数传入的,把它从参数列表中删除,因为调用方法时并不需要它
arguments = Array.prototype.slice.call(arguments, 1);
} else if( typeof(method) == 'object' || !method ) {
method = methods.init;
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.pluginName' );
return this;
}
// 用apply方法来调用我们的方法并传入参数
return method.apply(this, arguments);
}
})(jQuery);
var options = {
customParameter: 'Test 1',
propertyName: 'Test 2'
}
var defaults = {
propertyName: 'Test 3',
onSomeEvent: 'Test 4'
}
var settings = $.extend({}, defaults, options);
/*
settings == {
propertyName: 'Test 2',
onSomeEvent: 'Test 4',
customParameter: 'Test 1'
}
*/
(function($) {
var privateFunction = function() {
// 执行代码
}
var methods = {
init: function(options) {
// 在每个元素上执行方法
return this.each(function() {
var $this = $(this);
// 尝试去获取settings,如果不存在,则返回“undefined”
var settings = $this.data('pluginName');
// 如果获取settings失败,则根据options和default创建它
if(typeof(settings) == 'undefined') {
var defaults = {
propertyName: 'value',
onSomeEvent: function() {}
}
settings = $.extend({}, defaults, options);
// 保存我们新创建的settings
$this.data('pluginName', settings);
} else {
/ 如果我们获取了settings,则将它和options进行合并(这不是必须的,你可以选择不这样做)
settings = $.extend({}, settings, options);
// 如果你想每次都保存options,可以添加下面代码:
// $this.data('pluginName', settings);
}
// 执行代码
});
},
destroy: function(options) {
// 在每个元素中执行代码
return $(this).each(function() {
var $this = $(this);
// 执行代码
// 删除元素对应的数据
$this.removeData('pluginName');
});
},
val: function(options) {
// 这里的代码通过.eq(0)来获取选择器中的第一个元素的,我们或获取它的HTML内容作为我们的返回值
var someValue = this.eq(0).html();
// 返回值
return someValue;
}
};
$.fn.pluginName = function() {
var method = arguments[0];
if(methods[method]) {
method = methods[method];
arguments = Array.prototype.slice.call(arguments, 1);
} else if( typeof(method) == 'object' || !method ) {
method = methods.init;
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.pluginName' );
return this;
}
return method.apply(this, arguments);
}
})(jQuery);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有