【问题标题】:Why isn't MS Edge working with spread element and querySelector?为什么 MS Edge 不使用扩展元素和 querySelector?
【发布时间】:2023-03-27 01:36:01
【问题描述】:

在另一个发布的问题中:

var a = {};
a.products = [...document.querySelectorAll('.product')];
console.log(a.products);
<div class="product"> </div>

Edge 将失败并出现以下错误:

预期功能

但是这是有效的:

    var params = ['hello', '', 7];
    var other = [ 1, 2, ...params];

console.log(params);
console.log(other);

为什么排名靠前的不在 Edge 上运行(它在 Chrome 上运行)?

【问题讨论】:

  • 你试过[...(document.querySelectorAll('.product'))]吗?
  • 我按照你的建议做了,没用!
  • Edge 可能没有为NodeLists 实现迭代器协议? 编辑: 是的:developer.mozilla.org/en-US/docs/Web/API/…(至少它不支持entries()values() 等,如果它支持迭代器协议,我相信它会支持)。
  • 该错误消息并没有太大帮助,但它可能的意思是“预计document.querySelectorAll('.product')[Symbol.iterator] 是一个函数”。
  • 看起来节点列表在 Windows 10 Fall Creators 更新中可迭代:wpdev.uservoice.com/forums/257854-microsoft-edge-developer/…

标签: javascript ecmascript-6 microsoft-edge


【解决方案1】:

您可以使用Array.from,它从一个类似对象的数组中生成一个数组。

this.products = Array.from(document.querySelectorAll('.product'));

【讨论】:

【解决方案2】:

Bergi 和 Felix 似乎走在了正确的轨道上:在 MDN 上的 document 中,他们谈论了迭代器。

一些内置结构,例如扩展运算符,在底层使用相同的迭代协议:

所以 Array 在 Edge 中确实有 entries()nodelist 的地方不支持迭代。

Nina 的答案是 goto one!

【讨论】:

    【解决方案3】:

    更新到 2020 年,Edge 现在在内部使用 Chrome v8。 要求用户下载最新版本的 Edge。无需在旧 Edge 中处理此特定场景。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-11
      • 1970-01-01
      • 2012-10-24
      • 2021-02-07
      • 2021-05-13
      相关资源
      最近更新 更多