0

所以我有一个带有更新方法的插件,如果用户将新内容广告到 div(占位符),则在开始时一切正常,插件将使用插件的用户定义选项,但一旦用户运行update 方法它将使用默认选项而不是定义的选项。

插件包装...

;(function($, window, document, undefined){

    //"use strict"; // jshint ;_;

    var pluginName = 'myPlugin';

    function Plugin(element, options){

        this.elm     = $(element);
        this.options = $.extend({}, $.fn[pluginName].defaults, options);

        this.init();
    };

    Plugin.prototype = {

        init: function(){

            // we run this at start...

        },

        update: function(){

            // code that we need when we need to update...

        },

    };

    $.fn[pluginName] = function(option) {
        var options = typeof option == "object" && option;

        return this.each(function() {
            var $this = $(this);
            var data  = new Plugin($this, options);

            if(!$.data($this, pluginName)){         
                $.data($this, pluginName, data);
            }

            if(typeof option == 'string'){
                data[option]();
            }

        });
    };

    $.fn[pluginName].defaults = {
        name: 'Hank',
        ...
        ...
    };


})(jQuery, window, document);

//内联js

    $('#container').myPlugin({name: 'NOT Hank...'});

    $('#updatelink').click(function(e){
       $('#container').myPlugin('update');
    }):
4

0 回答 0