私はこれらの画像を変更しようとしています。配列にリンクを格納する必要がありますが、ボタンを押してイメージを変更すると、配列変数がイメージにリンクすることが認識されません。誰か助けてもらえますか?ここに私の現在のコードです:あなたは、文字列にsrc属性を設定している。この場合画像へのリンクを保持し、画像ソースで使用するためにJavaScript配列が必要です
<!DOCTYPE html>
<html>
<head>
<script>
var red = "https://s23.postimg.org/bo5a8hzsr/red_jpg.png"
var yellow = "https://s23.postimg.org/bo5a8hzsr/red_jpg.png"
var green = "https://s29.postimg.org/5ljr1ha3r/green.png"
var lights=["red","yellow","green"]
function changered()
{
var img = document.getElementById("light");
img.src="lights[0]";
return false;
}
function changeyellow()
{
var img = document.getElementById("light");
img.src="lights[1]";
return false;
}
function changegreen()
{
var img = document.getElementById("light");
img.src="lights[2]";
return false;
}
</script>
</head>
<body>
<button id="red" onclick="setTimeout(changeyellow, 3000),setTimeout(changered, 6000),setTimeout(changegreen, 12000)">automatic</button>
<button id="red" onclick="changered();">red</button>
<button id="yellow" onclick="changeyellow();">yellow</button>
<button id="green" onclick="changegreen();">green</button>
<img id="light" src="https://s29.postimg.org/5ljr1ha3r/green.png" />
<br><br><br>
</body>
</html>
、あなただけの配列を使用する必要があります。 – Cruiser
言い換えれば 'lights = [red、yellow、green]'と 'img.src = lights [0];'など – j08691