0

我正在使用普通变量来显示图像列表。但我需要的同样是使用 XML。谁能帮帮我吗。var 'imgs' 应该是一个 xml。

jQuery

$(function() {

    var imgs = ['http://www.academy-florists.com/images/shop/thumbnails%5CValentines_Day_flowers.jpg', 'http://www.everythingbuttheprincess.com/assets/images/babies-in-bloom-fuchsia-flower_thumbnail.jpg', 'http://www.behok.ru/i/a/cat/gerbera.jpg', 'http://www.thebutterflygrove.com/images/thumbnails/0/200/200/thumbnail_flower-decor-makpk.jpg', 'http://gameinfestedent.com/gallery_photo/medium_image/image1322820610_MainPurpleOrchids3_1a.jpg'];

   var maximages = imgs.length; //No of Images
   Slider();
   setInterval(Slider, 3000);
   var prevIndex = 0, prevPrevIndex = 0;

   function Slider() {
       $('#imageSlide').fadeOut("slow", function() {
           do {
               shuffleIndex = Math.floor(Math.random() * maximages);
           } while(prevIndex == shuffleIndex || prevPrevIndex == shuffleIndex)

           prevPrevIndex = prevIndex;
           prevIndex = shuffleIndex;

           $("#panel").fadeIn("slow").css('background', '#000');

           $(this).attr('src', imgs[shuffleIndex]).fadeIn("slow");
       });
   }

});
4

2 回答 2

2

我假设你想要 xml 格式的图像 url,然后你想要解析这个 xml。

<images>
<image-url>http://www.academyflorists.com/images/shop/thumbnails%5CValentines_Day_flowers.jpg</image-url>
<image-url>http://www.everythingbuttheprincess.com/assets/images/babies-in-bloom-fuchsia-flower_thumbnail.jpg</image-url>
<image-url>http://www.behok.ru/i/a/cat/gerbera.jpg</image-url>
<image-url>http://www.thebutterflygrove.com/images/thumbnails/0/200/200/thumbnail_flower-decor-makpk.jpg</image-url>
<image-url>http://gameinfestedent.com/gallery_photo/medium_image/image1322820610_MainPurpleOrchids3_1a.jpg</image-url>
</images>

您可以通过以下方式解析它

var imgs = "<images><image-url>'http://www.academy-florists.com/images/shop/thumbnails%5CValentines_Day_flowers.jpg'</image-url><image-url>'http://www.everythingbuttheprincess.com/assets/images/babies-in-bloom-fuchsia-flower_thumbnail.jpg'</image-url><image-url>'http://www.behok.ru/i/a/cat/gerbera.jpg'</image-url><image-url>'http://www.thebutterflygrove.com/images/thumbnails/0/200/200/thumbnail_flower-decor-makpk.jpg'</image-url><image-url>'http://gameinfestedent.com/gallery_photo/medium_image/image1322820610_MainPurpleOrchids3_1a.jpg'</image-url></images>"
$xml = $.parseXML(imgs)
images = []
$($($xml).find('image-url')).each(function() { images.push($(this).text()) })

images将包含所有图像 url 的数组

于 2012-07-30T06:26:46.647 回答
1

您还没有告诉我们您的 xml 会是什么样子,所以我将以下基本架构放在一起

<?xml version="1.0" encoding="utf-8" ?>
<images>
    <image>url of image</image>
    <image>url of next image</image>
<images>

如果您搜索 jQuery 站点,您会发现它告诉您如何使用 Xml - http://api.jquery.com/jQuery.parseXML/

您需要parseXML在 xml 字符串上使用并转换为 jQuery 对象。然后,您可以find()元素、循环元素或挑选元素文本和属性。以下示例显示了这一点。

$(function() {

    // your image list as xml string
    var xmlStr = '<?xml version="1.0" encoding="utf-8" ?><images><image>http://www.academy-florists.com/images/shop/thumbnails%5CValentines_Day_flowers.jpg</image><image>http://www.everythingbuttheprincess.com/assets/images/babies-in-bloom-fuchsia-flower_thumbnail.jpg</image><image>http://www.behok.ru/i/a/cat/gerbera.jpg</image><image>http://www.thebutterflygrove.com/images/thumbnails/0/200/200/thumbnail_flower-decor-makpk.jpg</image><image>http://gameinfestedent.com/gallery_photo/medium_image/image1322820610_MainPurpleOrchids3_1a.jpg</image></images>';

    // parse the xml and produce an jQuery image object ($image) to represent the list of image elements
    var xmlDoc = $.parseXML(xmlStr),
        $xml = $(xmlDoc),
        $image = $xml.find("image");

    // loop throught your image elements and alert the url
    $image.each(function() {
        var imageElement = this, // this within the loop is the current element
            $imageElement = $(imageElement),
            imageUrl = $imageElement.text(); // use text to get the url

        alert(imageUrl);
    });

    // use length on the jQuery object to get the element count
    var maximages = $image.length;
    alert(maximages);

    var shuffleIndex = 3,
        imageElement = $image[shuffleIndex], // index the random image that you want to use
        $imageElement = $(imageElement),
        imageUrl = $imageElement.text();

    alert(imageUrl);

});
于 2012-07-30T06:39:22.010 回答