JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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;
}

大佬总结

以上是大佬教程为你收集整理的javascript – 通过数组时文本变为未定义全部内容,希望文章能够帮你解决javascript – 通过数组时文本变为未定义所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: