【问题标题】:Additional values to function功能的附加值
【发布时间】:2018-04-24 07:39:30
【问题描述】:

如何向函数添加附加值?推送还是合并?

jQuery('#col_6907134').cycle({fx: 'fade', speed: 1000, timeout: 6500});

Values:
prev: '#prev'
next: '#next'

【问题讨论】:

    标签: jquery merge push


    【解决方案1】:

    您可以使用extend 来连接多个 json 对象

    $(function(){
    
        var default_values = {fx: 'fade', speed: 1000, timeout: 6500};
        var new_values = {
            prev : '#prev',
            next : '#next',
        };    
        var extended = $.extend( default_values, new_values );
    
        jQuery('#col_6907134').cycle(extended);
    
    });
    

    【讨论】:

    • 我不能改变这个jQuery('#col_6907134').cycle({fx: 'fade', speed: 1000, timeout: 6500});我可以在不改变函数的情况下添加值吗?
    • 您不需要更改插件,像我回答的那样调用它,该功能将替换并且分页将正常工作@FabianW
    • 在插件库之后添加该脚本
    【解决方案2】:

    此代码创建提到的功能:

    $.fn.cycle = function(options, arg2) {
    var o = { s: this.selector, c: this.context };
    
    // in 1.3+ we can fix mistakes with the ready state
    if (this.length === 0 && options != 'stop') {
        if (!$.isReady && o.s) {
            log('DOM not ready, queuing slideshow');
            $(function() {
                $(o.s,o.c).cycle(options,arg2);
            });
            return this;
        }
        // is your DOM ready?  http://docs.jquery.com/Tutorials:Introducing_$(document).ready()
        log('terminating; zero elements found by selector' + ($.isReady ? '' : ' (DOM not ready)'));
        return this;
    }
    
    // iterate the matched nodeset
    return this.each(function() {
        var opts = handleArguments(this, options, arg2);
        if (opts === false)
            return;
    
        opts.updateActivePagerLink = opts.updateActivePagerLink || $.fn.cycle.updateActivePagerLink;
    
        // stop existing slideshow for this container (if there is one)
        if (this.cycleTimeout)
            clearTimeout(this.cycleTimeout);
        this.cycleTimeout = this.cyclePause = 0;
        this.cycleStop = 0; // issue #108
    
        var $cont = $(this);
        var $slides = opts.slideExpr ? $(opts.slideExpr, this) : $cont.children();
        var els = $slides.get();
    
        if (els.length < 2) {
            log('terminating; too few slides: ' + els.length);
            return;
        }
    
        var opts2 = buildOptions($cont, $slides, els, opts, o);
        if (opts2 === false)
            return;
    
        var startTime = opts2.continuous ? 10 : getTimeout(els[opts2.currSlide], els[opts2.nextSlide], opts2, !opts2.backwards);
    
        // if it's an auto slideshow, kick it off
        if (startTime) {
            startTime += (opts2.delay || 0);
            if (startTime < 10)
                startTime = 10;
            debug('first timeout: ' + startTime);
            this.cycleTimeout = setTimeout(function(){go(els,opts2,0,!opts.backwards);}, startTime);
        }
    });
    

    };

    结果:

    jQuery('#col_6907134').cycle({fx: 'fade', speed: 1000, timeout: 6500});
    

    如何在不更改上层代码的情况下通过函数添加附加值:

        var new_values = {
        prev : '#prev',
        next : '#next',
    };   
    

    【讨论】:

      猜你喜欢
      • 2015-12-31
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多