2015-11-06 28 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接受这个答案,如果它可以帮助你。 –