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();
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容