今、私はWebプログラミング、特に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;
}
}
おそらくこれを行うための最良の方法ではありませんが、このコードは最初は機能します。ただし、3番目の画像をクリックして最初の画像に戻ると、画像は正常に機能しますが、テキストは「未定義」になります。私は周りを検索しましたが、このコードで本当に「間違った」ものを見つけることができないようです。
どんな助けでも大歓迎です。