Changing multiple images on JavaScript

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

<!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> 

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.

回答1:

You are using "innerHTML" to "img" tag, you have to use "src" attribute to make it work:

document.getElementById('myImage').src = tLight[1] 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!