var arr = document.querySelectorAll("a[href*='somestring']")返回控制台中的数组。正方形括号[]和arr.length = 7。
下面的屏幕。为什么拼接()不工作在我的数组上?

发布于 2015-11-12 15:51:17
从querySelectorAll返回的对象是一个NodeList,它类似于数组,但不是一个实际的数组。
尝试将其转换为数组:
[].slice.call(document.querySelectorAll("a[href*='somestring']"));发布于 2015-11-12 15:51:52
HTMLCollection和NodeList对象没有splice方法,也不继承Array.prototype。
此外,您不能简单地调用它们上的拼接,因为即使它们类似于数组,它们也不会被设计为被修改。
首先,将它们转换为真正的Array。
var arr = document.querySelectorAll("a[href*='somestring']"); // NodeList
arr = Array.prototype.slice.call(arr); // Array
arr.splice(2, 2); // splicing an Array发布于 2015-11-12 16:14:50
document.querySelectorAll("a[href*='somestring']")返回一个对象而不是数组。
尝试将其转换为数组:
var arr = document.querySelectorAll("a[href*='somestring']");
var a = [];
for(var i =0;i<arr.length ; i++){
a[i] = arr[i];
}
a.splice()//now you can use a as an arrayhttps://stackoverflow.com/questions/33675568
复制相似问题