<input type="text" id="imgUrl" value=""> <br> <input type="button" value="播放" onclick="autoPlay()"> <input type="button" value="添加圖片" onclick="addImage()"><br> <img src="" alt=""? height="400px" width="400px" id="img"><br> <script> var imageArr = ['http://eduppp.cn/images/0/1.jpg','http://eduppp.cn/images/0/2.jpg', 'http://eduppp.cn/images/0/3.jpg','http://eduppp.cn/images/0/4.jpg']; var index=0; function autoPlay(){ var imgObj=document.getElementById('img'); //imgObj.style.display='inline'; if(imageArr.length<=0){ imgObj.alt = '圖片木有了,請?zhí)砑?; } else{ index = (index+1)%imageArr.length;//變量值隨數(shù)組長度變化而變化 imgObj.src = imageArr[index]; ? ? ?} setTimeout(autoPlay,2000) //console.log(imageArr[index]) }index = (index+1)%imageArr.length;這句代碼什么意思?還有有沒有可替代的方案實(shí)現(xiàn)這種效果,for循環(huán)為什么就不行?
index = (index+1)%imageArr.length;這句代碼什么意思?還有有沒有
慕村2502594
2018-12-31 19:45:46