就是我现在的代码有点啰嗦,我要怎么改,才可以以后可以增加图片,但是也不需要改代码。我大概知道要用length,但是我不知道怎么弄
var imgs = ['1.jpg', '2.jpg','3.jpg'];
// 显示图片的数量
var index = 0;
function setImage(imgCount){
index += imgCount;
if (index > imgs.length) {
index = imgs.length;
return;
}
if (index < 0) {
index = 0;
return;
}
document.getElementById("img1").setAttribute('src', imgs[(index) % 3]);
document.getElementById("img2").setAttribute('src', imgs[(index + 1) % 3]);
document.getElementById("img3").setAttribute('src', imgs[(index + 2) % 3]);
}
function myFunction()
{