Home >Web Front-end >JS Tutorial >JavaScript tutorial--click to cycle through pictures
This article introduces you to the effect of clicking to cycle through js through the example code. Friends who need it can refer to it
No more nonsense, I will directly post the code for you. The specific code is as follows:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>单击循环切换图片</title> <script> var i = 1; function nextImg(){ i++; //步骤2:获取页面元素 var next = document.getElementById("nImg"); //步骤3:更改元素的src属性 next.src = "img/"+i+".jpg"; if(i==3){ i=0; } } </script> </head> <body> <p> <!--步骤1:绑定onclick事件--> <button onclick="nextImg()">下一张</button> <br /> <!--需自己准备3张图片--> <img src="img/1.jpg" id="nImg" width="600px" height="400px" /> </p> </body> </html>
The above is the detailed content of JavaScript tutorial--click to cycle through pictures. For more information, please follow other related articles on the PHP Chinese website!