1

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

// 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 );

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

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

4

1 回答 1

0

原来我需要打电话;

self.loadingBar( self.options.duration ); 

self.loadingBar( options.duration ); 

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

于 2012-05-23T16:13:57.070 回答