Javascript ForEach功能在IE中不起作用

编程入门 行业动态 更新时间:2024-10-12 01:32:17
本文介绍了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); });

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

Regards,

解决方案

forEach 的mozilla/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: jsfiddle/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中不起作用

本文发布于:2023-11-27 22:36:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639882.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   功能   Javascript   ForEach

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!