Pass additional parameters to jQuery each() callback

后端 未结 4 672
梦谈多话
梦谈多话 2020-12-24 05:32

I\'m working on an app that will present surveys to the user. The markup looks something like this:


    
4条回答
  •  有刺的猬
    2020-12-24 06:16

    Though this may not be relevant answer , But since the question is how to pass parameter to jQuery each function .

    First Approach : Using Partial Function - more on this and from closure library

    jsfiddle:

    var param = 'extra-param';   
    
    var partial = function(fn, var_args) {
      var args = Array.prototype.slice.call(arguments, 1);
      return function() {
        // Clone the array (with slice()) and append additional arguments
        // to the existing arguments.
        var newArgs = args.slice();
        newArgs.push.apply(newArgs, arguments);
        return fn.apply(this, newArgs);
      };
    };
    
    $(['joe','james','rick','kirk']).each(partial (function (index,value) {
        alert(arguments.length);
        alert(arguments[0]);
     },param));
    

    Second Approach is

    $.each function can take either 2 parameters Index and Element(which can also be referred as this) OR if you do not need Index then you can pass Array as argument to $.each and element can referred as this

    CAUTION : args is for internal use only - by jQuery

    // Execute a callback for every element in the matched set.
    // (You can seed the arguments with an array of args, but this is
    // only used internally.)
    each: function( callback, args ) {
        return jQuery.each( this, callback, args );
    },
    

    Which will call each: function( obj, callback, args )

    then call callback.apply( obj[ i ], args ); if you pass array as argument otherwise callback.call( obj[ i ], i, obj[ i ] );

    please note the difference of apply and call

    Sample code : http://jsfiddle.net/dekajp/yA89R/1/

    var param = 'rick';
    $(['joe','james','rick','kirk']).each(function (arg1 , arg2 ,arg3) {
        //alert('[this: '+this +'] [arg1: '+ arg1 +'] [arg2:'+arg2+'] [param:'+param+']');
    },['hello 1','hello 2','hello 3']);
    

    For Reference jQuery Each Code version 1.9

    // args is for internal usage only
        each: function( obj, callback, args ) {
            var value,
                i = 0,
                length = obj.length,
                isArray = isArraylike( obj );
    
            if ( args ) {
                if ( isArray ) {
                    for ( ; i < length; i++ ) {
                        value = callback.apply( obj[ i ], args );
    
                        if ( value === false ) {
                            break;
                        }
                    }
                } else {
                    for ( i in obj ) {
                        value = callback.apply( obj[ i ], args );
    
                        if ( value === false ) {
                            break;
                        }
                    }
                }
    
            // A special, fast, case for the most common use of each
            } else {
                if ( isArray ) {
                    for ( ; i < length; i++ ) {
                        value = callback.call( obj[ i ], i, obj[ i ] );
    
                        if ( value === false ) {
                            break;
                        }
                    }
                } else {
                    for ( i in obj ) {
                        value = callback.call( obj[ i ], i, obj[ i ] );
    
                        if ( value === false ) {
                            break;
                        }
                    }
                }
            }
    
            return obj;
        },
    

提交回复
热议问题