动态使用插件中的jQuery选项

时间:2012-03-14 09:03:12

标签: jquery plugins

不确定我是否正确接近......

基本上我有一个插件结构:

(function($){

$.fn.xxxxx = function(options) {

    var o = {
        widgets: 'a,b,c',
        a: {
            id: 'abc',
            title: 'ABC'
        },
        b: {
            id: 'def',
            title: 'DEF'
        },
        c: {
            id: 'ghi',
            title: 'GHI'
        }
    };

    var options = $.extend(o, options);

    return this.each(function(options){

        var myplugin = {
            init: function(){
                var x = o.widgets.split(',');
                $.each(x, function(i,v){
                    myplugin.widgets(i,v);
                });
            },
            widgets: function(i,v){

                var t = o.v.title ? '<h3>'+o.v.title+'</h3>' : '' ;

            }
        }
        myplugin.init();
    });
};

})(jQuery的);

是否可以使用o.widgets选项中的字符串值然后调用其中一个o.a,o.b,o.c选项?

基本上,a,b或c将传递给小部件功能,然后用于分配选项值。以下是我想要它做的,但它显然不会那样:

var t = o.v.title ? '<h3>'+o.v.title+'</h3>' : '' ;

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

嗯,关于Javascript的一个好处是,以下是等价的:

foo.bar === foo['bar'];

因此,如果您想访问o.a,可以通过o['a']访问它。 更进一步,您可以设置如下功能:

function herp (opt) {
    return o[opt];
}

...并将其称为var foo = herp('a');或其他内容。

相关问题