2010-11-24 31 views
1

建庫我擁有一個充滿圖像鏈接一個數組,我想編寫一個腳本,將,使用JavaScript(用jQuery庫),將格式化HTML輸出像這樣:jQuery的從陣列

<div id="gallery"> 
<div class="scrollable"> 
    <div class="items"> 
     <div> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
     </div> 
    </div> 
</div> 
</div> 

在每處理5個圖像鏈接後,它會在items類中添加另一個div容器。因此,如果數組包含15個圖像鏈接,那麼最終結果將如下所示:

<div id="gallery"> 
<div class="scrollable"> 
    <div class="items"> 
     <div> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
     </div> 
     <div> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
     </div> 
     <div> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
      <img src="URL_from_array"/> 
     </div> 
    </div> 
</div> 
</div> 

等等,直到數組結束。有任何想法嗎?

編輯:對代碼標識感到抱歉,它發佈時,它搞砸了。

編輯2:澄清。

+0

是不是能夠做到這一點的服務器端,而不是* *後的頁面加載? – 2010-11-24 03:15:32

+0

是的,這是可能的,但我想出於各種原因做客戶端。 – 2010-11-24 03:32:36

回答

0

*編輯 - 我修正了代碼,以便動態創建更多的圖像,以五個爲一組。我相信這就是你所問的一切。

這個怎麼樣?

$('div#gallery img').each(function(){ 
    var i = $('div#gallery img').index(this); 
    $(this).attr('src',images[i]); 
}); 

嘗試demo