类扩展:
(function ($) {
$.extend({
max:function (a, b) {return (a > b ? a : b);},
add:function (a, b) {return a+b;}
})
})(jQuery)
//调用方法:
//console.log("野薇薇"+ $.add(2,3));
//console.log("野薇薇"+ $.max(2,3));
//独立命名,防止冲突
(function ($) {
$.MyPlugin = {
max:function (a, b) {return (a > b ? a : b);},
add:function (a, b) {return a+b;}
}
})(jQuery)
//调用方法:
//console.log("野薇薇"+ $.MyPlugin.add(2,88));
//console.log("野薇薇"+ $.MyPlugin.max(99,3));
对象扩展:
(function ($) {
$.fn.changeColor = function () {this.css("color","red");return this};
$.fn.setFontSize = function () {this.css("fontSize","30px"); return this}
})(jQuery)
//$("p").changeColor().setFontSize();
(function ($) {
$.fn.extend({
changeColor : function () {this.css("color","blue");return this},
setFontSize : function () {this.css("fontSize","100px"); return this}
})
})(jQuery)
//$("p").changeColor().setFontSize();
网友评论