Javascript ForEach功能在IE中不起作用 [英] Javascript ForEach Function does not work in IE

查看:612
本文介绍了Javascript ForEach功能在IE中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能写出下面的代码,它支持所有的浏览器?
因为IE8中不支持forEach函数...
$ b $ pre $ digits.forEach(function(值,索引){
//创建具有初始条件的跨度
var span = $('< span>',{
'class':'digit0',
'data':{
'current':0,
'goal':value
}
});
//追加span到div#number
span.appendTo($('div#number'));
//在间隔乘以此跨度的索引后调用countUp
setTimeout(function(){countUp.call(span) ;},index * interval);
});

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



Regards,

解决方案

forEach 的mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach\">MDN文档包括使用方法的两个实现在实现JS早期版本的浏览器中。



我将在这里复制一个快速的(见完整链接):

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


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,

解决方案

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

这篇关于Javascript ForEach功能在IE中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆