JQuery插件

类扩展:

(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();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容