更改多个图像 [英] Changing multiple images

查看:156
本文介绍了更改多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到过一段我一直在研究的代码问题。我被要求创建一个数组,并通过一个按钮循环通过红绿灯,这就是我所做的:

I've been having problems recently with a piece of code that I have been working on. I am asked to create an array and cycle through the traffic lights with a button, this is what I have done:

<!DOCTYPE html>
<html>


<head>
<title> Traffic Lights </title>
</head>

<body>

<script>
whatPosition=1;
var images=["redlight.gif","redandamber.gif","greenlight.gif","amberlight.gif"];
function changeImage() {

if (whatPosition==0) {
document.getElementById("image").innerHTML=images[1];
whatPosition=1;

} 

else if (whatPosition==1){
document.getElementById("image").innerHTML=images[2]
whatPosition=2;
alert("I am an alert box!");
}
else if (whatPosition==2){
document.getElementById("image").innerHTML=images[3]
whatPosition=3;
}
else if (whatPosition==3){
document.getElementById("image").innerHTML=images[4]
whatPosition=4;
}
else if (whatPosition==4){
document.getElementById("image").innerHTML=images[0]
whatPosition=0;


推荐答案

图片没有 innerHTML ,试试这个:

document.getElementById(image)。src = images [0];

这篇关于更改多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆