Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在jQuery中为插件创建嵌套选项_Javascript_Jquery_Css_Jquery Plugins - Fatal编程技术网

Javascript 如何在jQuery中为插件创建嵌套选项

Javascript 如何在jQuery中为插件创建嵌套选项,javascript,jquery,css,jquery-plugins,Javascript,Jquery,Css,Jquery Plugins,我知道如何使用插件,但如何使用嵌套选项,如: var defaults = { spacing:10, shorten_to:50, from_start:0, from_end:2, classes: { test:'testing' } }; 我知道这不对,我只是不知道当我想做这样的事情时如何编写正确的语法: 欢迎其他建议,我需要定制类名的能力,共有7个,因此我不希望像上面的例子那样让测试类、示例类等变得更干净整洁。事实上这是

我知道如何使用插件,但如何使用嵌套选项,如:

var defaults = {
    spacing:10,
    shorten_to:50,
    from_start:0,
    from_end:2,
    classes: {
        test:'testing'
    }
};
我知道这不对,我只是不知道当我想做这样的事情时如何编写正确的语法:


欢迎其他建议,我需要定制类名的能力,共有7个,因此我不希望像上面的例子那样让测试类、示例类等变得更干净整洁。

事实上这是正确的。您的符号称为JSON,它是一种非常简单的符号(请参阅)

相当于

var someobject = { prop: 'prop', { name: 'name' }};
在第二个示例中,缺少一个冒号

$('#breadcrumbs').breadcrumbs({classes:{test:'new_example'},spacing:12})

您的插件使用一个选项参数,用户使用对象文本将参数传递到插件中。然后使用$.extend将选项与默认值组合在一起。这是一个可以复制的插件模式

//Create closure
(function($) {

    var defaults = { //Default settings for breadcrumbs
        async: false,
        race: 100,
        interval: 1,
        classes: {
            test:'testing'
        }
    };

    //Plugin definition
    $.extend({

        //Execute the functions added to the stack
        breadcrumbs: function(options) {

            options = $.extend(true, defaults, options);

            //Loop through each item in the matched set and apply event handlers
            return this.each(function(i) {

                //Code here , this = current selection
            });
        }
    });

// end of closure and execute
})(jQuery);
您可以这样称呼这个插件

$('div').breadcrumbs({async:true, interval:2, classes: {another: true}});

我有所有这些,我很好奇如何嵌套默认值:)好的。已更新。$。扩展应该做一个“深扩展”。太棒了,谢谢。。。但是唯一的问题是在扩展中添加了{}之后,它没有改变默认值?$('top breadcrumbs').breadcrumbs({classes:{breadcrumb_item:'the_bc',delay_time:100});例如,breadcrumb_项仍然=='breadcrumb item'@Oscar,@James,应该是
$.extend(true,默认值,选项)。deep是一个布尔per。谢谢,非常接近,但是现在,如果我不设置所有它们,它们都是,但是定义的,未定义的。例如:$('top breadcrumbs')。breadcrumbs({classes:{breadcrumb_item:'the_bc'},delay_time:100});使每个项都未定义,但面包屑项除外。我希望它像普通的jQuery默认值或自定义值一样工作way@Oscar为此,您需要Jquery的扩展函数和深度复制,如James所示。
//Create closure
(function($) {

    var defaults = { //Default settings for breadcrumbs
        async: false,
        race: 100,
        interval: 1,
        classes: {
            test:'testing'
        }
    };

    //Plugin definition
    $.extend({

        //Execute the functions added to the stack
        breadcrumbs: function(options) {

            options = $.extend(true, defaults, options);

            //Loop through each item in the matched set and apply event handlers
            return this.each(function(i) {

                //Code here , this = current selection
            });
        }
    });

// end of closure and execute
})(jQuery);
$('div').breadcrumbs({async:true, interval:2, classes: {another: true}});