不能将拼接与 querySelectorAll 返回的 NodeList 结合使用

Cannot use splice with NodeList returned by querySelectorAll

我正在使用 querySelectorAll 获取一个 NodeList(本质上是一个类似数组的对象),如下所示:

var elements = document.getElementById('something').querySelectorAll('.lazy'); 

然后,我尝试使用 splice:

从该 NodeList 中删除一项
elements.splice(0, 1);

我收到以下错误:

elements.splice is not a function.

如何从 NodeList 中删除元素?或者,我应该将 NodeList 的每个元素存储在一个数组中并使用拼接吗?

NodeList 不是 Array 或其子类型,因此它无法访问 Array.prototype.splice.

如果要使用splice,可以先转成数组:

var elementArray = Array.prototype.slice.call(elements);

编辑:抱歉,拼接和切片混淆了。