简体   繁体   English

选项内的jQuery插件设置选项

[英]jQuery plug-in set options inside an option

Not sure if my Title is the right way to ask how to do this but... 不确定我的头衔是否是询问如何执行此操作的正确方法,但是...
Here i have a plug-in called func which sets default options: 在这里,我有一个名为func的插件,用于设置默认选项:

(function($){
$.fn.func= function(options){

    var settings = $.extend({
        color:'white',
        hoverColor: 'black'
    },options);

    return this.each(function(){
        if(settings.color){
            $(this).css('color',settings.color);
        }
        if(settings.hoverColor){
            $(this).css('color',settings.hoverColor);
        }
    }

Now here is where the user can change the color of the text to what they want: 现在,用户可以在此处将文本的颜色更改为所需的颜色:

$('#selector').func({
    color:'blue',
    hoverColor: 'green'
});

Now here is what i want to accomplish: I want to be able to break up the hover and link options to look like the following: 现在,这是我要完成的任务:我希望能够分解悬停和链接选项,使其看起来像以下内容:

$('#selector').func({
    link:
        color: 'blue'
    ,hover:
        color: 'green'
});

Similar to jQuery validation plug-in HERE where they break up the rules and the message options. 与jQuery验证插件HERE类似, 此处它们分解了规则和消息选项。 Let me know if you need additional information. 让我知道您是否需要其他信息。 Thanks for any and all help. 感谢您提供的所有帮助。

The syntax in what you want to accomplish is invalid so you will always get a syntax error. 您想要完成的语法无效,因此您总是会遇到语法错误。

Try 尝试

$('#selector').func({
    link: {color: 'blue'},
    hover: {color: 'green'}
});

Of course you will have to use the same structure in the initial settings object ( use the deep copy version of $.extend , by providing true as the first argument, since we are using objects ) 当然,您必须在初始settings对象中使用相同的结构( 使用$.extend的深层复制版本,因为将第一个参数提供为true,因为我们正在使用对象

var settings = $.extend(true, {
        link: {color: 'white'},
        hover: {color: 'black'}
    },options);

and you access them with settings.link.color and settings.hover.color 您可以使用settings.link.colorsettings.hover.color访问它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM