Javascript ForEach函数在IE中不起作用

Sim*_*mon 2 javascript foreach jquery function internet-explorer-8

我怎么能写下以下所有浏览器都支持的代码?因为似乎IE8中不支持forEach-Function ...

    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 );
} );
Run Code Online (Sandbox Code Playgroud)

请在此处查看完整的代码:http://jsfiddle.net/bBadM/(它不适用于所有浏览器)在此先感谢.

问候,

Que*_*tin 10

对MDN文档forEach包括在实现早期版本的JS的浏览器所使用的方法的两种实现方法.

我将在这里重现快速的(请参阅完整的链接):

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);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)