js按鈕切換圖片案例
阿新 • • 發佈:2018-12-11
js修改img標籤src屬性的值
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>切換圖片</title> <style> div{ border: 1px solid white; width:500px ; height: 350px; margin: auto; text-align: center; } </style> <script> var i=1; function changeImg(){ i++; document.getElementById("img1").src="../../img/"+i+".jpg"; if(i==3){ i=0; } } </script> </head> <body> <div> <input type="button" value="下一張" onclick="changeImg()"/> <img src="../../img/1.jpg" width="100%" height="100%" id="img1"/> </div> </body> </html>