基本格式
;(function($){
$.fn.extend({
“<{method name}>":function(value){
}
})
})(jQuery);
========================================================================================
例子:
#颜色插件 color();
1》设置匹配元素颜色
2》获取该元素颜色
;(function($){
$.fn.extend({
"color":function(value){
if ( value == undefined ) {
return this.css('color');
}else{
return this.css('color',value);
};
}
})
})(jQuery);
========================================================================================
我们经常到碰类似 animate()这类方法,里面的参数为对象,可以同时设置多个参数。
扩展:参数为对象时
;(function($){
$.fn.extend({
"color":function(options){
var type = typeof(options);
for( i in options){
switch(i)
{
case 'color':
this.css('color',options.color);
break;
case 'background':
this.css('background',options.background);
break;
default:
return false;
};
};
}
});
})(jQuery);
========================================================================================
封装类似css()这类参数即可能为字符串也可能为对象的方法的时候,只需要判断一下参数的类型就OK拉。
本文转自:武汉前端培训