gpt4 book ai didi

Javascript 数组、图像链接、链接

转载 作者:行者123 更新时间:2023-12-03 06:41:38 26 4
gpt4 key购买 nike

<!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 http://stackoverflow.com/questions/13739568/how-do-i-link-a-javascript-file-to-a-html-file -->
</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 = color[0];

此外,您的 color["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>

关于Javascript 数组、图像链接、链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37931218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com