我知道有一個錯誤(我試圖預覽在瀏覽器上我的代碼和按鈕無法正常工作),但我不能發現它,並希望儘可能使我的代碼更高效。如何使用一個按鈕(HTML/JS)兩個圖像之間進行切換
<!DOCTYPE html>
<html>
<body>
<h1>Mood Changer</h1>
<button onclick="ChangeImage()">Change Mood</button>
<img id="myImage" src="Images/sad.png" style="width:100px">
<script>
var index = 0;
var ImageList = newArray(1);
ImageList[0] = "Images/sad.png";
ImageList[1] = "Images/smiley.png";
function ChangeImage()
{
index = index + 1;
If (index ==ImageList.length)
{
index = 0;
}
var image1 = document.getElementById("myImage");
image1.src = ImageList[index];
}
</script>
</body>
</html>