gpt4 book ai didi

javascript - array.splice() 给出错误 TypeError : arr. splice 不是函数(…)

转载 作者:行者123 更新时间:2023-11-28 11:11:58 26 4
gpt4 key购买 nike

var arr = document.querySelectorAll("a[href*='somestring']")

返回控制台中看起来像数组的内容。方括号 [] 和 arr.length = 7。

屏幕如下。为什么 splice() 对我的数组不起作用? enter image description here

最佳答案

querySelectorAll返回的对象是一个NodeList,它类似于Array,但不是一个实际的数组。

尝试将其转换为数组:

[].slice.call(document.querySelectorAll("a[href*='somestring']"));

https://developer.mozilla.org/en-US/docs/Web/API/NodeList

关于javascript - array.splice() 给出错误 TypeError : arr. splice 不是函数(…),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33675568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com