Javascript 数组,图像链接,链接

Javascript array, image links, linking

<!DOCTYPE html>
<html>
<head>
<title>Controlled Assessment!</title>
<link rel="stylesheet" href="Css/stylesheet.css" type="text/css"/>
<!-- This is the webpage i found how to link an external script  -->
</head>

<body>

    <img id="color" src="Pictures/green.jpg" >

    <p id="hel">he</p>

    <button onclick="nxt()">new colour</button>

    <script type="text/jscript" src="JavaScript/trafficlight.js" />
</body>


<footer>

    <script src="JavaScript/trafficlight.js"></script>

</footer>


</html>

那是我的 html 代码

//My array that will be used for the traffic light sequence
var colour = ["Pictures/red.jpg", "Pictures/green.jpg", "Pictures/amber.jpg"];

function nxt() {

"use strict";
document.getElementById("hel").innerHTML = "helllll";

document.getElementById("color").innerHTML = colour["Pictures/red.jpg"];
}

这是我的 Javascript 我想做的是,当您单击按钮时,图像会更改为不同的图像,在本例中 javascript 部分的数组中列出试图从绿色变为红色,但它不起作用,我真的很困惑为什么??

要更改图像的 src,您可以执行以下操作: document.getElementById("color").src = 颜色[0];

此外,您的颜色["Pictures/red.jpg"] 没有多大意义。 通过您想要的元素的 id 查找它。

//My array that will be used for the traffic light sequence
var colour = ["Pictures/red.jpg", "Pictures/green.jpg", "Pictures/amber.jpg"];

function nxt() {
document.getElementById("hel").innerHTML = "helllll";

document.getElementById("color").src = colour[0];
}
    <img id="color" src="Pictures/green.jpg" >

    <p id="hel">he</p>

    <button onclick="nxt()">new colour</button>