对于每个循环,除了Firefox之外,并不适用于主流浏览器 [英] for each loop is not working in major browsers except firefox

查看:129
本文介绍了对于每个循环,除了Firefox之外,并不适用于主流浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

<script type="text/javascript">
for each ( var movie in ['Endhiran The Robot','Shivaji The Boss','Anbulla Rajinikanth'] )
    document.writeln(movies + "<br/>");
</script>

在Firefox中,我将输出作为

In Firefox, I am getting the output as

Endhiran The Robot
Shivaji The Boss
Anbulla Rajinikanth

但在 IE9 Chrome版本23.0.1271.95 m 中,对于每个都不起作用,文档上也没有写入任何内容

But in IE9 and Chrome Version 23.0.1271.95 m, for each is not working and nothing is written on the document

推荐答案

$ c>对于JavaScript中的每个,这是一个非标准的扩展。

There is no for each in JavaScript, that was a non-standard extension.

有新的 ES5 forEach function ,这在现代浏览器中得到了很好的支持,尽管当然不是老版本:
$ b

There is the new ES5 forEach function, which is well supported in modern browsers although of course not in older ones:

['Endhiran The Robot','Shivaji The Boss','Anbulla Rajinikanth'].forEach(function(movie) {
    document.writeln(movie + "<br/>");
});

这个其他答案在这里堆栈溢出。 (这个问题提到了jQuery,但答案涵盖了普通的JavaScript和jQuery选项。)

Several other options over in this other answer here on Stack Overflow. (The question mentions jQuery, but the answer covers both plain JavaScript and jQuery options.)

这篇关于对于每个循环,除了Firefox之外,并不适用于主流浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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