【问题标题】:jQuery Plugin; Can't work defaults outjQuery插件;无法正常工作
【发布时间】:2012-05-28 02:06:51
【问题描述】:

如何让这个插件代码接受默认选项?

// Utility
if ( typeof Object.create !== 'function' ) {
object.create = function( obj ) {
    function F(){};
    F.prototype = obj;
    return new F();
};
}

(function( $, window, document, undefined ) {
var Super = {
    init: function( options, elem ) {
        var self = this;

        self.elem = elem;
        self.$elem = $( elem );

        //self.alertbox( options.alertmsg );    
        self.loadingBar( options.duration );

        if ( typeof options === 'string' ) {
            self.search = options;
        } else {
            // object was passed
            self.search = options.search;
        }


        self.options = $.extend({}, $.fn.superHero.options, options );


    },

    alertbox: function( message ) {
        var self = this;

        alert(message);

    },

    loadingBar: function( duration ) {
        var self = this;

        $('#loadingBar').animate({
            width:  699             
        }, duration);



    },


};

$.fn.superHero = function( options ){
    return this.each(function() {

        var hero = Object.create( Super );
        hero.init( options, this );

        $.data( this, 'superHero', hero);

    });

};

$.fn.superHero.options = {
    alertmsg:   'Hello World!',
    duration:   8000,
};

})( jQuery, window, document );

仅当我在调用插件时在索引页面中定义它们时,这些选项才有效。

如何使选项具有由用户覆盖而不是用户要求的默认值?

【问题讨论】:

  • 原来我需要打电话; self.loadingBar(self.options.duration);而不是 self.loadingBar( options.duration );不知道有什么区别;我只知道它有效。

标签: jquery plugins default options


【解决方案1】:

原来我需要打电话;

self.loadingBar( self.options.duration ); 

相对于

self.loadingBar( options.duration ); 

不知道有什么区别;我只知道它有效。

【讨论】:

    猜你喜欢
    • 2011-07-28
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多