javascript 为什么 forEach() 在 IE11 的 iframe 中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27065659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:54:23  来源:igfitidea点击:

Why does forEach() not work in an iframe in IE11?

javascripthtmliframeforeachinternet-explorer-11

提问by Tamás Bolvári

This question was closed as off-topic, so I've markedthe missinginfo.

这个问题被关闭为题外话,所以我标记缺失的信息。

Specific problem or errorplus shortest code neccesary:

特定问题或错误加上必要的最短代码:

The code below doesn't work in IE11, if it's in an iframe of certain websites. ("Certain" is not specific, but I don't have a public demo. I can't make one, until I find the cause in my private code. Still, the question is specific enough to be answered by an expert, that's why I've asked SO instead of a long debugging process without any idea.)

下面的代码在 IE11 中不起作用,如果它在某些网站的 iframe 中。(“某些”不具体,但我没有公开演示。我无法制作一个,直到我在我的私人代码中找到原因。不过,这个问题足够具体,可以由专家回答,那就是为什么我问 SO 而不是一个漫长的调试过程而没有任何想法。)

['a', 'b'].forEach(function(elem){console.log(elem);});

The errorsays that the array doesn't support the forEach method.

错误表示该数组不支持 forEach 方法。

Desired behavior:

期望的行为:

The forEach() method executes a provided function once per array element. - MDN

forEach() 方法为每个数组元素执行一次提供的函数。- MDN

回答by Tamás Bolvári

"IE11 uses Quirks Mode emulationif the top-level page is not in Edge Mode." - MSDN

“如果顶级页面不在边缘模式下,IE11 将使用 Quirks 模式模拟。” - MSDN

In this mode, arrays don't support the forEach method.

在这种模式下,数组不支持 forEach 方法。

Use a simple for loopinstead, or write this right after the title tag of the parent:

改用一个简单的for 循环,或者在父级的标题标签之后写这个:

<meta http-equiv="X-UA-Compatible" content="IE=edge" />