Javascript 未捕获的类型错误。indexOf不是函数错误
我是一个新手,试图创建js脚本来隐藏和显示网页上的部分。前面的部分似乎工作正常。代码段的最后一部分返回以下错误:Javascript 未捕获的类型错误。indexOf不是函数错误,javascript,indexof,Javascript,Indexof,我是一个新手,试图创建js脚本来隐藏和显示网页上的部分。前面的部分似乎工作正常。代码段的最后一部分返回以下错误: scripts.js:29 Uncaught TypeError: sections.indexOf is not a function at nextSection (scripts.js:29) at <anonymous>:1:1 scripts.js:29未捕获类型错误:sections.indexOf不是函数 在下一节(scripts.js:2
scripts.js:29 Uncaught TypeError: sections.indexOf is not a function
at nextSection (scripts.js:29)
at <anonymous>:1:1
scripts.js:29未捕获类型错误:sections.indexOf不是函数
在下一节(scripts.js:29)
时间:1:1
有人能告诉我我在这里没有得到什么吗
/*
============================================
array of all sections
============================================
*/
var sections=document.querySelectorAll("section");
/*
============================================
Function to find the section that is being displayed (the one that doesn't have "not1st" as a class.)
============================================
*/
function findSection(){
for (var i=0; i<sections.length; i++) {
if (sections[i].className.includes("not1st")){
continue;
} else {
return sections[i];
}}}
/*
============================================
Function to load the next section
============================================
*/
function nextSection() {
sections[sections.indexOf(findSection())+1];
}
/*
============================================
所有节的数组
============================================
*/
var sections=document.queryselectoral(“section”);
/*
============================================
函数查找正在显示的部分(没有“not1st”作为类的部分)
============================================
*/
函数findSection(){
对于(var i=0;iquerySelectorAll
不返回数组,它返回一个NodeList
NodeList
s没有indexOf
函数
您可以使用数组将其转换为数组。从:
var sections = Array.from(document.querySelectorAll("section"));
…或对于没有数组的旧浏览器。从开始,数组#切片:
var sections = Array.prototype.slice.call(document.querySelectorAll("section"));
有关更多信息,请参阅我的另一个答案中的“For Array Like Objects”部分。我知道最后一部分还不完整,但我会一步一步地尝试确保每一点都正常工作,然后再继续编写代码。因为您没有数组…querySelectorAll是一个HTML集合。