【问题标题】:IE 11 AngularJS error - Object doesn't support property or method 'from'IE 11 AngularJS 错误 - 对象不支持“来自”的属性或方法
【发布时间】:2019-01-28 05:28:54
【问题描述】:

在我的 JavaScript 中,我有一个函数 detectEnvironmentAndGetLEAndDepot(),它通过 HTML 调用 onload。我正在使用 wicket,需要获取后端保存的值,所以我将它们发射到屏幕上,隐藏它们,然后从我的 JS 中搜索 <span> 值以获取 name 值,例如 @ 987654325@ 或 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 类有关。非常感谢您的帮助。

【问题讨论】:

    标签: javascript html angularjs internet-explorer-11


    【解决方案1】:

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

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

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

    【讨论】:

    • 这似乎有效。但是,我将代码改回 Array.from - 它也有效。我的应用程序看起来非常喜怒无常(在 IE 中),有时它可以工作,有时它不能。最重要的是,只要我打开开发人员工具,一切都会中断……我什至没有起始位置。我讨厌 IE。
    • @notAChance 我从来没有使用过 Angular,但是我在寻求相同问题的帮助时遇到了this(也使用了Array.from())。听起来您的代码气质可能与同一问题有关;在错误的时间/加载文档之前加载。
    【解决方案2】:

    您可以使用 ES2015 polyfill,例如 es6-shimArray.fromBabel polyfill

    【讨论】:

      【解决方案3】:

      正如 Mozilla here 所解释的,IE 尚不支持 Array.from 功能

      【讨论】:

        【解决方案4】:

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

        仅供参考:

        以下文档模式不支持“Array.from”:Quirks、Internet Explorer 6 标准、Internet Explorer 7 标准、Internet Explorer 8 标准,Internet Explorer 9 标准,Internet Explorer 10 标准,Internet Explorer 11 标准。在 Windows 中不支持 8.1.

        source

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-02-24
          • 1970-01-01
          • 2014-11-06
          • 2021-02-13
          • 2019-07-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多