JS练习:替换式图片自动轮播
代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script> var index = 0; function changeImg() { // 获得要切换图片的那个元素 var img = document.getElementById("img1"); //计算出当前要切换到第几张图片 var curIndex = index % 3 + 1; //0,1,2 img.src = "img/" + curIndex + ".jpg"; //1,2,3 //每切换完,索引加1 index = index + 1; } function init() { setInterval("changeImg()", 3000); } </script> </head> <body onload="init()"> <img src="img/1.jpg" width="1920" id="img1"/> </body> </html>