【问题标题】:Javascript ForEach Function does not work in IEJavascript ForEach 函数在 IE 中不起作用
【发布时间】:2013-01-27 10:56:18
【问题描述】:

如何编写所有浏览器都支持的以下代码? 因为好像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 );
} );

在此处查看完整代码http://jsfiddle.net/bBadM/(并非适用于所有浏览器) 提前致谢。

问候,

【问题讨论】:

  • Internet Explorer 不支持“for each”循环。您需要更改代码以使用常规 for 循环:
  • 你可以使用纯 javascript for 循环来实现这个,jQuery 正在停止支持像 IE8 这样的旧浏览器
  • 既然你已经用过jQuery,那就用$.each()吧。

标签: javascript jquery function internet-explorer-8 foreach


【解决方案1】:

MDN documentation for 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);
    }
  }
}

【讨论】:

  • 尤其是 Internet Explorer 11!加油,微软!
猜你喜欢
  • 2011-08-31
  • 2010-10-20
  • 2017-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-03
  • 1970-01-01
相关资源
最近更新 更多