2012-01-11 102 views
0

我拉圖像陣列出來一個JSON文件的,我有HTML的img我在哪裏放置圖像的src中:如何獲得個人項目從一個數組的 - jQuery的

HTML:

<img class="photos" src="" alt="" /> 

JS:

for (var i = 0, len = data.length; i < len; i++) { 
    var $div = $(".venue:first").clone(); 
    data.[i].photos; 
    $div.find(".photos").attr("src", data[i].photos); 
} 

[編輯]

JSON: 
comedyData([ 
    { 
     "name": "Broadway Comedy Club", 
     "checkinsCount": 2695, 
     "tipCount": 36, 
     "phone": "2127572323", 
     "canonicalUrl": "https://foursquare.com/v/broadway-comedy-club/4afcb158f964a520212522e3", 
     "mayor": "David M.", 
     "mayor_photo": "https://img-s.foursquare.com/userpix_thumbs/1DFPPSPYD3N2DHBA.jpg", 
     "address": "318 West 53rd St, New York, NY 10019", 
     "photos": [ 
      "https://img-s.foursquare.com/derived_pix/0UZLALW0AOLS5NECIKPJQJ15BEUOTFSASYTF3JPWOPO4S130_100x100.jpg", 
      "https://img-s.foursquare.com/derived_pix/MMP15NDCGLEC2GKJZEHIVLWLUXJ2IVZHM0DR1Z2K22MQF0PR_100x100.jpg", 
      "https://img-s.foursquare.com/derived_pix/RNW1WQQS3BKGMAJT3VV5RJ5XDBQ4FJ1WAGBREWLAWWSM41VB_100x100.jpg", 
      "https://img-s.foursquare.com/derived_pix/RI1LT5EUNKFN3303TRI23F0WKCCICOIHNGXTTXDLDEIH454V_100x100.jpg", 
      "https://img-s.foursquare.com/derived_pix/0VACIGDCLHHS4FKZO13EDO5SCOCAKGLRE04DSR3B0WT3E0X0_100x100.jpg", 
      "https://img-s.foursquare.com/derived_pix/Q2GHCRHSUEUL3AQELVE3KSMP3SMUBDXBWIGXAVSMEKPDVPAQ_100x100.jpg" 
     ] 
    } 

我的問題是它把圖像數組放在圖像屬性中,但我只想要一個一個。我不知道如何從數組中獲取單個圖像並將它們放在html圖像的src中?

+0

你能告訴我們你的JSON文件是什麼樣子? – 2012-01-11 15:15:54

+0

我可以請看看你正在拉的json以及周圍的html – mcgrailm 2012-01-11 15:16:37

+0

請提供JSON的結構,即數據。所以,我們可以引導你。 – 2012-01-11 15:16:47

回答

1

你可以提供一個function(i, attr)到您.attr()

for (var i = 0, len = data.length; i < len; i++) { 
    var $div = $(".venue:first").clone(); 
    data.[i].photos; 
    $div.find(".photos").attr("src", function(index, src){ 
     return data[i].photos[index]; 
    }); 
} 
+0

這似乎工作! – Xtian 2012-01-11 15:20:45

相關問題