admin管理员组

文章数量:1296893

how could I write the following Code that it is supported in all browsers? Because it seems that the forEach-Function is not supported in IE8...

    digits.forEach( function( value, index ) {
    // create a span with initial conditions
    var span = $( '<span>', {
        'class': 'digit0',
        'data': {
            'current': 0,
            'goal' : value
        }
    } );
    // append span to the div#number
    span.appendTo( $( 'div#number' ) );
    // call countUp after interval multiplied by the index of this span
    setTimeout( function() { countUp.call( span ); }, index * interval );
} );

See the full Code here: / (it´s not working with all browsers) Thanks in advance.

Regards,

how could I write the following Code that it is supported in all browsers? Because it seems that the forEach-Function is not supported in IE8...

    digits.forEach( function( value, index ) {
    // create a span with initial conditions
    var span = $( '<span>', {
        'class': 'digit0',
        'data': {
            'current': 0,
            'goal' : value
        }
    } );
    // append span to the div#number
    span.appendTo( $( 'div#number' ) );
    // call countUp after interval multiplied by the index of this span
    setTimeout( function() { countUp.call( span ); }, index * interval );
} );

See the full Code here: http://jsfiddle/bBadM/ (it´s not working with all browsers) Thanks in advance.

Regards,

Share asked Feb 12, 2013 at 7:18 SimonSimon 4672 gold badges12 silver badges31 bronze badges 3
  • Internet Explorer doesn't support "for each" loops. You will need to change the code to use regular for loops: – Rinku Commented Feb 12, 2013 at 7:20
  • You can use pure javascript for loop for this, and jQuery are ending support of old browsers like IE8 – Taron Mehrabyan Commented Feb 12, 2013 at 7:21
  • 4 Since you already use jQuery, use $.each(). – JJJ Commented Feb 12, 2013 at 7:21
Add a ment  | 

1 Answer 1

Reset to default 10

The MDN documentation for forEach includes two implementations of the method for use in browsers that implement earlier versions of JS.

I'll reproduce the quick one (see the link for the plete one) here:

if ( !Array.prototype.forEach ) {
  Array.prototype.forEach = function(fn, scope) {
    for(var i = 0, len = this.length; i < len; ++i) {
      fn.call(scope, this[i], i, this);
    }
  }
}

本文标签: jqueryJavascript ForEach Function does not work in IEStack Overflow