简体   繁体   English

首先从xml中读取照片链接,然后使用Photoswipe打开它

[英]First read photo link from xml and use Photoswipe to open it

What I want to do is to save the link of photo into an array and than pass it to the photoswiper so that my photo can be viewed by photoswiper. 我要做的是将照片的链接保存到数组中,然后将其传递给photowiper,以便photowiper可以查看我的照片。

Unfortunitely, the jquery xml parse can't change the global variable. 不幸的是,jQuery xml解析无法更改全局变量。 I can change the value of the global variable inside the parseXml(xml) function. 我可以在parseXml(xml)函数中更改全局变量的值。 Then I can pass the link to the function(window, PhotoSwipe). 然后,我可以将链接传递给函数(窗口,PhotoSwipe)。

 var cars = new Array();

 var carcar;

$(document).ready(function()
{
$.ajax({
    type: "GET",
    url: "http://www.utravel.com.hk/mobile-app/news-photo-xml.php?id=3256",
    dataType: "xml",
    success: function(xml) { parseXml(xml); }
        });
});

function parseXml(xml)
{ 
$(xml).find("item").each(function()
{  
carcar="http://www.utravel.com.hk/cms/news_photo/original/"+$(this).find("photo").text().substring(5);
        cars.push(carcar);   

});

}               
    (function(window, PhotoSwipe){

        document.addEventListener('DOMContentLoaded', function(){

            var
                options = {
                    preventHide: true,
                    getImageSource: function(obj){
                        return obj.url;
                    },
                    getImageCaption: function(obj){
                        return obj.caption;
                    }
                },
                instance = PhotoSwipe.attach( 
                    [
                        { url: 'cars[0]', caption: 'Image 001'},
                        { url: 'cars[1]', caption: 'Image 002'},
                        { url: 'cars[2]', caption: 'Image 003'},
                    ], 
                    options 
                );

                instance.show(0);

        }, false);

    }(window, window.Code.PhotoSwipe));

I think that by now you probably solved your problem, but your URLs shouldn't be between colons, should be like this: 我认为到现在您可能已经解决了问题,但是您的网址不应该在冒号之间,应该像这样:

{ url: cars[0], caption: 'Image 001'},
{ url: cars[1], caption: 'Image 002'},
{ url: cars[2], caption: 'Image 003'},

Otherwise, what you are doing is saying that the URL is actually cars[x] which of course its never gonna be found. 否则,您正在说的是URL实际上是cars[x] ,当然永远也找不到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM