微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
}

原文地址:https://www.jb51.cc/js/429651.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐