2012-11-09 65 views
0

我有2個按鈕。每個按鈕都顯示不同的數組。一切都很好,除非我想回到第一次重新加載圖像。所以當我第一次點擊按鈕時,我的6張圖片就出現了。第二次是12.等等。有沒有辦法阻止這種情況發生?似乎無法在網上找到任何東西。使用JavaScript,如何停止圖像重新加載?

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'; 

     } 
    } 

回答

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. 
相關問題