0

我有 2 个按钮。每个按钮显示一个不同的数组。一切都很好,除了说我想回到它重新加载图像的第一个。所以当我第一次点击按钮时,我的 6 张图片就会显示出来。第二次共12个。以此类推。有没有办法阻止这种情况发生?好像在网上找不到任何东西。

var picArray1 = new Array("concert1","concert2","concert3","concert4","concert5","concert6");
var picArray2 = new Array("out1","out2","out3","out4","out5","out6");


function showArray1() {
        var imgDiv = document.getElementById("gallery")
        for(i=0; i<picArray1.length; i++){
            document.getElementById("text1").innerHTML += '<img src="images/'+picArray1[i]+'.jpg">';
            document.getElementById("text2").style.display = 'none';
            document.getElementById("text1").style.display = 'block';
        }
    }

function showArray2() {
        var imgDiv = document.getElementById("gallery")
        for(i=0; i<picArray2.length; i++){
            document.getElementById("text2").innerHTML += '<img src="images/'+picArray2[i]+'.jpg">';
            document.getElementById("text1").style.display = 'none';
            document.getElementById("text2").style.display = 'block';

        }
    }
4

1 回答 1

0

让我们做一些分解:

function show(list,galleryId,text1Id,text2Id) {
        var imgDiv = document.getElementById(galleryId)
        var text1 = document.getElementById(text1Id);
        var text2 = document.getElementById(text2Id);
        text2.innerHTML = "" // important part
        for(i=0; i<list.length; i++){
            text2.innerHTML += '<img src="images/'+list[i]+'.jpg">';
            text1.style.display = 'none';
            text2.style.display = 'block';

        }
    }
// call the function
show(['image1','image2','image3'],"gallery","text1","text2")
// not sure why you are getting imgDiv though since you are not using it.
于 2012-11-09T01:09:57.183 回答