Javascript ForEach函数在IE中不起作用

Javascript ForEach函数在IE中不起作用,javascript,jquery,function,internet-explorer-8,foreach,Javascript,Jquery,Function,Internet Explorer 8,Foreach,如何编写所有浏览器都支持的以下代码? 因为在IE8中似乎不支持forEach函数 digits.forEach( function( value, index ) { // create a span with initial conditions var span = $( '<span>', { 'class': 'digit0', 'data': { 'current': 0,

如何编写所有浏览器都支持的以下代码? 因为在IE8中似乎不支持forEach函数

    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 );
} );
digits.forEach(函数(值、索引){
//使用初始条件创建跨度
变量范围=$(''{
“类”:“数字0”,
“数据”:{
“当前”:0,
“目标”:价值
}
} );
//将span追加到div#编号
span.appendTo($('div#number');
//间隔乘以此跨度的索引后调用countUp
setTimeout(函数(){countUp.call(span);},索引*间隔);
} );
请参见此处的完整代码:(它不适用于所有浏览器) 提前谢谢

在这方面,

包括两种方法的实现,用于实现早期版本JS的浏览器

我将在这里复制快速版本(完整版本请参见链接):

if(!Array.prototype.forEach){
Array.prototype.forEach=函数(fn,范围){
对于(变量i=0,len=this.length;i
Internet Explorer不支持“for each”循环。您需要将代码更改为使用常规for循环:您可以使用纯javascript for循环来实现这一点,而jQuery将不再支持像IE8这样的旧浏览器,因为您已经使用jQuery,请使用。特别是Internet Explorer 11!来吧,微软!
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);
    }
  }
}