Mudar uma Imagem com um click | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Mudar uma Imagem com um click

Como faço para mudar uma imagem com um click? ex:Luz apagada ai eu clico e substituo ela por outra imagem no caso uma luz acesa

19th Sep 2016, 11:59 PM
Gileade Lima
Gileade Lima - avatar
1 Answer
+ 2
<!DOCTYPE html> <html> <head> <script> var mysrc = "mars.jpg"; function changeImage() { if (mysrc == "mars.jpg") { document.images["pic"].src = "http://www.fluidoweb.com/images/SoloLearn/mars.jpg"; document.images["pic"].alt = "Mars"; mysrc = "earth.jpg"; } else { document.images["pic"].src = "http://www.fluidoweb.com/images/SoloLearn/earth.jpg"; document.images["pic"].alt = "Earth"; mysrc = "mars.jpg"; } } </script> </head> <body> <img src="http://www.fluidoweb.com/images/SoloLearn/earth.jpg" alt="Earth" id="pic" class="portrait" onclick="changeImage()" width="464px" height="298px"> </body> </html>
20th Sep 2016, 8:17 AM
Richard Blackburn
Richard Blackburn - avatar