简体   繁体   English

jQuery插件的动态数组

[英]Dynamic Arrays for Jquery Plugins

I am building a simple JQuery plugin in which a user can build modules that contain a title, subtitle, and text. 我正在构建一个简单的JQuery插件,用户可以在其中构建包含标题,字幕和文本的模块。

I want to allow the user to be able to add multiple elements to the module in one plugin call. 我想允许用户能够在一个插件调用中向模块添加多个元素。

var methods = {
    build : function( options ) {
    var settings = $.extend( {
        'header'    : 'Untitled',
        'subtitle' : 'Subtitle',
        'content' : 'Lorem ipsum dolor amet...',
        'img' : ''  //img URL
    }, options);

    //Create the div to hold elements
    var box = $('<div/>', {
        class: 'service'
    }).appendTo(this);

    //Populate div with header
        $('<h2/>', {
        class: 'service-title',
        text: settings.header
    }).appendTo(box);

    //Check for subtitle
    if(settings.subtitle != 'Subtitle') {
        $('<h4/>', {
            class: 'service-sub',
            text: settings.subtitle
        }).appendTo(box);
    }

    //Add body text
    $('<p/>', {
        class: 'service-text',
        text: settings.content
    }).appendTo(box);

    //Check for image
    if(settings.img != '') {
        $('<img/>', {
            class: 'service-img',
            src: settings.img
        }).appendTo(box);
    }


    },
    add : function() {

    }
};

          $.fn.service = function( method ) {
                if ( methods[method] ) {
                    return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1));
                } else if ( typeof method === 'object' || ! method ) {
                    return methods.build.apply( this, arguments );
                } else {
                    $.error( "Method " + method + " does not exist for the Servicer" );
                }
          };
        })( jQuery );

In the variable settings, under subtitle, I would like to allow users to add more than one subtitle within one call to .service() 在变量设置的字幕下,我想允许用户在一次调用.service()的过程中添加多个字幕。

Allow user to make subtitle option an array instead of string. 允许用户将字幕选项设置为数组而不是字符串。 Test if it is a string or array ,if it is an array run a loop. 测试它是字符串还是数组,如果是数组,请运行循环。

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

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