2011-05-05 16 views
1

我想創建一個基於xml的圖像顯示與點擊按鈕。on click隨機從XML與JavaScript

所以讓我在外部XML文件中說,我有以下幾點:

<?xml version="1.0" encoding="utf-8"?> 
<gallery> 
    <photo name="summer"> 
     <file>imageone.jpg</file> 
    </photo> 
    <photo name="winter"> 
     <file>image2.jpg</file> 
    </photo> 
    <photo name="sprint"> 
     <file>3.jpg</file> 
    </photo> 
    <photo name="fall"> 
     <file>four.jpg</file> 
    </photo> 
</gallery> 

的網頁,我想有一個按鈕,當點擊它會從上面的列表,並顯示一個洗牌圖像隨同相應的照片名稱節點一起隨機播放。

我已經看過各種jQuery的XML解析器,但它有randomizin它的麻煩。這可能嗎?

謝謝!

回答

2

就產生一個隨機號碼檢索你的照片後陣,並挑選一個:

$photos = $(xmldata).find('photo'); 
$randomImage = $photos.eq(Math.floor(Math.random() * $photos.length)); 

$name = $randomImage.attr('name'); 
$img = $randomImage.find('file').text(); 
0

我會建議:

1) read all image names with any parser into array 
2) randomize the array with jQuery 
3) select and use 1