不知道出了什么问题(我对JavaScript很陌生),所以请帮助我!我的JavaScript代码中出现了TypeError: “x” is not a function,如何解决呢?

D //Error Area, Uncaught TypeError: images is not a function 
file:///C:/Users/user/Desktop/LounaIndex/scriptIndex.js:19 
EventListener.handleEvent* 
file:///C:/Users/user/Desktop/LounaIndex/scriptIndex.js:14
我的代码如下:

var images = [
    "cat_pics/Louna (1).jpeg",
    "cat_pics/Louna (2).jpeg",
    "cat_pics/Louna (3).jpeg",
    "cat_pics/Louna (4).jpeg",
    "cat_pics/Louna (6).jpeg",
    "cat_pics/Louna (7).jpeg"
];
var btn = document.querySelector("button")
var img = document.querySelector("img")
var counter = 1;
btn.addEventListener("click", function () {
    if (counter === 8) {
        counter = 0
    }

    img.src = images(counter)
    counter = counter + 1;
});

您的代码中几乎不需要更改,例如 images数组长度为6,并且如果条件处理多于6个元素

if (counter === 8) { // it should be like counter === 6
    counter = 0
}
需要在img.src = images(counter) 此images变量中进行第二次更改,array因此应使用类似img.src = images[counter]

您可以在下面看到完整的示例

试试看 HTML

<button>changeImage</button><img />
JavaScript代码
var images = [
    "cat_pics/Louna (1).jpeg",
    "cat_pics/Louna (2).jpeg",
    "cat_pics/Louna (3).jpeg",
    "cat_pics/Louna (4).jpeg",
    "cat_pics/Louna (6).jpeg",
    "cat_pics/Louna (7).jpeg"
];
var btn = document.querySelector("button")
var img = document.querySelector("img")
var counter = 1;
btn.addEventListener("click", function () {
    if (counter === 6) {
        counter = 0
    }
    img.src = images[counter]
    counter = counter + 1;
});
为了更动态地处理,您应该使用if条件来判断
if(counter === images.length){
    counter = 0
}