Array.prototype.splice.call - 不使用 querySelectorAll('.className')

Array.prototype.splice.call - not working with a querySelectorAll('.className')

我有三个 div 的 class 名称 .column ,在底部,在我的 body 标签之前我有一个小脚本应该得到这些 divs 并将它们放入一个数组中,以便我可以处理它们。我以前这样做过,所以我不知道是什么原因导致了这个问题这次

var columns = Array.prototype.splice.call(document.querySelectorAll('.column'));
document.write("Size: " + columns.length);

上面的代码特别是 Array.prototype.splice.call(document.querySelectorAll('.column')); 应该将 querySelectorAll 返回的节点列表变成一个可用的数组。但是,每当我写 var columns 的长度时,它总是 returns 0,这是不可能的,因为我有三个 div class。 现在,当我这样做时:

var columnsNodes = document.querySelectorAll('.column'); document.write(columnsNodes.length);

而且我把它写到文件里呢returns3,这是正确的。这让我相信关于将节点列表转换为数组的某些操作不起作用,这很奇怪,因为我之前已经多次这样做过。有谁知道是什么原因造成的?

我做过的事情:

没有任何效果,请提供您的意见。

The splice() method adds/removes items to/from an array, and returns the removed item(s).

Note: This method changes the original array.

http://www.w3schools.com/jsref/jsref_splice.asp

您应该改用 slice(),这样它就不会尝试编写 NodeList(这是不可能的)。