2017-10-10 117 views
10

在发布另外一个问题,这是有:MS Edge为什么不使用spread元素和querySelector?

this.products = [...document.querySelectorAll('.product')]; 

边缘将失败,并出现以下错误:

function expected

但是这是工作:

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

为什么没有顶一个在Edge上工作(它在Chrome上)?

+1

你试过'[...(document.querySelectorAll('。product'))]'? –

+1

我对您的建议做了,不工作! – Mouser

+5

Edge可能不会为'NodeList'实现迭代器协议? *编辑:*是:https://developer.mozilla.org/en-US/docs/Web/API/NodeList#Browser_compatibility(至少它不支持'entries()','values()'等,如果它支持迭代器协议,我相信它会。)。 –

回答

7

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

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

无论如何都是用于类型转换的[首选语法](https://stackoverflow.com/a/40549565/1048572) – Bergi

1

那么它看起来像BERGI和Felix在正确的轨道上:在这个document上MDN他们谈论的迭代器。

Some built-in constructs, such as the spread operator, use the same iteration protocol under the hood:

那么,阵确实有entries()在边缘一nodelist不和不支持迭代。

妮娜的答案是转到一个!

相关问题