最近我一直在用一段我一直在努力的代碼遇到問題。我要求創建通過交通燈帶按鈕的排列和循環,這是我做了什麼:更改多張圖片
<!DOCTYPE html>
<html>
<head>
<title> Traffic Lights </title>
</head>
<body>
<script>
whatPosition=1;
var images=["redlight.gif","redandamber.gif","greenlight.gif","amberlight.gif"];
function changeImage() {
if (whatPosition==0) {
document.getElementById("image").innerHTML=images[1];
whatPosition=1;
}
else if (whatPosition==1){
document.getElementById("image").innerHTML=images[2]
whatPosition=2;
alert("I am an alert box!");
}
else if (whatPosition==2){
document.getElementById("image").innerHTML=images[3]
whatPosition=3;
}
else if (whatPosition==3){
document.getElementById("image").innerHTML=images[4]
whatPosition=4;
}
else if (whatPosition==4){
document.getElementById("image").innerHTML=images[0]
whatPosition=0;
圖像不使用'innerHTML',因爲它們不包含html,你可能是想設置'src'屬性 –
Kenny,不要破壞你的問題。 – Cerbrus