Javascript ForEach Function does not work in IE

血红的双手。 提交于 2020-01-24 13:52:26

问题


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.net/bBadM/ (it´s not working with all browsers) Thanks in advance.

Regards,


回答1:


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 complete 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);
    }
  }
}


来源:https://stackoverflow.com/questions/14827406/javascript-foreach-function-does-not-work-in-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!