我试图创建一个使用函数数组来循环执行顺序的程序。我已经包括下面的程序,有人可以请建议我做错了什么。我已经创建了一套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完全不同。 –
我总是想知道为什么那些不是那么复杂的不完整问题在发布后的几秒钟内就投了票。 –