2015-11-06 49 views
-1

我想插入多個img循環當我點擊,圖像的名稱是「0.jpg」,「1.jpg」,「2.jpg」等。錯誤nan插入img與循環

For這個我用蒙山所有元素的數組:

{name: '1', result:1, prefecture: "city", photo1:"0.jpg"}, 

和我的循環

$('.map area').click(function(){ 
    index = $(this).index(); 
    var dir = questions[index].name; 
    var div = document.getElementById("tooltip_img"); 
    var ul = document.getElementById("gallery"); 

     for (var i = 0; i < 5; i+= 1) { 
      var img = document.createElement("IMG"); 
      var li = document.createElement('li'); 
      var a = document.createElement('a'); 
      a.setAttribute("class", "fancybox"); 
      a.setAttribute('rel', 'gallery1'); 
      a.setAttribute('id', i); 
      img.setAttribute("src",questions[index].photo1); 

      ul.appendChild(li) 
      li.appendChild(a) 
      a.appendChild(img) 
      document.getElementById('container').appendChild(div); 
     } 
}); 

蒙山這樣的:img.setAttribute("src",questions[index].photo1);,它的工作,因爲我稱之爲「照片1」。

但是如果我想使用

img.setAttribute("src",questions[index].photo+i); 

我有一個男的錯誤「網:: ERR_FILE_NOT_FOUND」

爲什麼我不能使用呼叫圖像索引?

感謝很多

回答

0

嘗試

img.setAttribute("src",questions[index]["photo"+i]); 
+0

十分感謝! – Snake

+0

@Snake接受這個答案,如果它可以幫助你。 –