jQuery插件;无法正常工作

时间:2012-05-21 14:48:46

标签: jquery plugins default options

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

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

只有在调用插件时在索引页面中定义选项时,这些选项才有效。

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

1 个答案:

答案 0 :(得分:0)

事实证明我需要打电话;

self.loadingBar( self.options.duration ); 

而不是

self.loadingBar( options.duration ); 

不确定区别是什么;我知道它是否有效。