在JavaScript上更改多个图像 [英] Changing multiple images on JavaScript
本文介绍了在JavaScript上更改多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<!DOCTYPE html>
<html>
<head>
<script>
position=0;
var tLight = ["red light.gif","red_and_amber_light.gif","green light.gif","amber light.gif"]
function changeImage() {
if (position==0) {
document.getElementById('myImage').innerHTML=tLight[1]
position=1;
}
else if (position==1) {
document.getElementById('myImage').innerHTML=tLight[2]
position=2;
}
else if (position==2) {
document.getElementById('myImage').innerHTML=tLight[3]
position=3;
}
else if (position==3) {
document.getElementById('myImage').innerHTML=tLight[4]
position=4;
}
}
</script>
</head>
<body>
<img id="myImage" src="red light.gif" width="100" height="180">
<button type="button"
onclick="changeImage()">click for da lite</button>
</body>
</html>
我似乎无法在此代码中找到问题,我不知道是否是我盲人还是.....?有人请帮忙。我查看了Chrome开发人员工具,但这无法检测到任何问题。
I cant seem to find the problem in this code and i don't know whether it is me being blind or.....? someone please help. I have looked at the chrome "Developer Tools" but this cant detect any issues.
推荐答案
您正在使用innerHTML来 imgtag,你必须使用src属性才能使它工作:
You are using "innerHTML" to "img" tag, you have to use "src" attribute to make it work:
document.getElementById('myImage').src = tLight[1]
这篇关于在JavaScript上更改多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文