通过数组时,文本变为未定义

Joh*_*gan 5 javascript arrays string

现在,我是网络编程的新手,特别是javascript.我正在尝试编写一个脚本,用于在用户单击图像时更新网页上的图像及其文本.这是代码:

//Images array
imgs = Array("test1.jpg", "test2.jpg", "test3.jpg");

//Names array
names = Array("Test1", "Test2", "Test3");

//Holds how many times our page has been clicked
var click = 0;

//Another click var
var click2 = 0;

//change function 
function change()
{

//Get the ID of 'nam', and start incrementing the elements in our array
document.getElementById("nam").innerHTML = names[++click2];

//Get an element with the ID 'first', and start incrementing the elements in  our      array
document.getElementById("first").src = imgs[++click];

//If the user clicks to the end of the gallery
if(click==2)
{
    click = -1;
}

if(click2==2)
{
    click = -1;
}

}
Run Code Online (Sandbox Code Playgroud)

可能不是最好的方法,但这段代码最初会起作用.但是,当我单击第三个图像返回到第一个图像时,图片工作正常,但文本变为"未定义".我一直在搜索,但我似乎无法找到任何与此代码完全"错误"的内容.

任何帮助表示赞赏.

小智 1

变量名称中的拼写错误:

//If the user clicks to the end of the gallery
if(click==2)
{
    click = -1;
}

if(click2==2)
{
    click = -1;
}
Run Code Online (Sandbox Code Playgroud)

应该

//If the user clicks to the end of the gallery
if(click==2)
{
    click = -1;
}

if(click2==2)
{
    click2 = -1;
}
Run Code Online (Sandbox Code Playgroud)