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

现在,我是网络编程的新手,特别是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;
}

}

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

任何帮助表示赞赏.

最佳答案
错误名称中的错字:

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

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

应该

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

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

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...