jquery 插件开发

一种

jQuery.extend({

    myAlert2:function (str1) {

        alert(str1);

    },

    myAlert3:function () {

        alert(11111);

    }

});

二种

;(function ($) {

    $.fn.plugin=function (options) {

        vardefaults={

            //各种参数、各种属性};

//options合并到defaults上,defaults继承了options上的各种属性和方法,将所有的赋值给endOptions

var endOptions=$.extend({},defaults, options);


        return this.each(function () {

            //实现功能的代码      

         });

    };

})(jQuery);

三种

//定义Beautifier的构造函数

var Beautifier = function(ele, opt) {

    this.$element = ele,

    this.defaults = {

        'color': 'red',

        'fontSize': '12px',

        'textDecoration':'none'

    },

    this.options = $.extend({}, this.defaults, opt)

}

//定义Beautifier的方法

Beautifier.prototype = {

    beautify: function() {

        return this.$element.css({

            'color': this.options.color,

            'fontSize': this.options.fontSize,

            'textDecoration': this.options.textDecoration

        });

    }

}

//在插件中使用Beautifier对象

$.fn.myPlugin = function(options) {

    //创建Beautifier的实体

    var beautifier = new Beautifier(this, options);

    //调用其方法

    return beautifier.beautify();

}

你可能感兴趣的:(jquery 插件开发)