IE 11 AngularJS 错误 - 对象不支持 属性 或方法 'from'

IE 11 AngularJS error - Object doesn't support property or method 'from'

在我的 JavaScript 中,我有一个函数 detectEnvironmentAndGetLEAndDepot(),它通过 HTML 调用 onload。我正在使用 wicket,需要获取后端保存的值,所以我将它们显示到屏幕上,隐藏它们,然后从我的 JS 中搜索 <span> 值以获得 name 值,例如 if(v.getAttribute('name') === 'LE'){do stuff}if(v.getAttribute('name') === 'depot'){do stuff}(我确定这不是最优雅的解决方案,但我需要一个快速的解决方案!)。然后在函数 detectEnvironmentAndGetLEAndDepot() 中我做了一些格式化等所以数据可用。

detectEnvironmentAndGetLEAndDepot() 函数(很长,只有相关部分)-

detectEnvironmentAndGetLEAndDepot = function() {

    Array.from(document.getElementsByTagName('span')).forEach(function(v) {

//Search name tag for particular names, then do formatting
}

当我在 IE11 中打开它时,我在弹出窗口 SCRIPT438: Object doesn't support property or method 'from' 中收到错误,这与上述方法的第一行 - Array class 有关。非常感谢帮助。

正如 Mozilla here 所解释的那样,IE

尚不支持 Array.from 功能

您可以使用 _underscore.js 和函数 _.toArray(document.getElementsByTagName('span'))...

仅供参考:

'Array.from' not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Not supported in Windows 8.1.

source

由于IE不支持Array.from方法,您可以尝试使用:

[].slice.call(document.getElementsByTagName('span')).forEach(function(v) {});

这不需要使用任何第 3 方库。

您可以使用 ES2015 polyfill,例如 es6-shim, Array.from or Babel polyfill