我試圖創建一個使用函數數組來循環執行順序的程序。我已經包括下面的程序,有人可以請建議我做錯了什麼。我已經創建了一套HTML上的交通燈,我試圖寫一些JavaScript,將改變單擊按鈕時顯示哪個燈。我已經創建了一組函數,用於確定我想要燈光出現的順序。我還編寫了將顯示每個燈光的功能。我是新來的JavaScript,任何幫助將不勝感激。在javascript中爲函數使用數組
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Task three</title>
<link href="Task 3-CSS.css" rel="stylesheet" type="text/css" />
<script src="Task3-Java.js"></script>
</head>
<body>
<div id="control_panel">
<button onclick="change_light">Change Light</button>
</div>
<div id="traffic_light">
<div id="red_light" class="light"></div>
<div id="amber_light" class="light"></div>
<div id="green_light" class="light"></div>
</div>
</body>
</html>
var light_array=[red,red_amber,green,amber];
var light_index = 0;
function no_light(){
document.getElementById('red_light').style.backgroundColor = "black";
document.getElementById('amber_light').style.backgroundColor = "black";
document.getElementById('green_light').style.backgroundColor = "black";
}
function red(){
no_light();
document.getElementById('red_light').style.backgroundColor="red";
}
function red_amber(){
no_light();
document.getElementById('red_light').style.backgroundColor="red";
document.getElementById('amber_light').style.backgroundColor="orange";
}
function green(){
no_light();
document.getElementById('green_light').style.backgroundColor="green";
}
function amber(){
no_light();
document.getElementById('amber_light').style.backgroundColor="orange";
}
function change_light(){
light_array[light_index](red,red_amber,green,amber);
light_index++;
if (light_index > 3){light_index = 0;}
}
change_light();
你能告訴我們你有什麼問題?你有錯誤嗎? –
你應該改變你的標籤sas,這不涉及Java。 Java和Javascript完全不同。 –
我總是想知道爲什麼那些不是那麼複雜的不完整問題在發佈後的幾秒鐘內就投了票。 –