0

我正在将我的 Flickr 集加载到我的网站中,如下所示:

$(window).load(function() {
var apiKey = 'xxx';
var userId = '8378546@N08';

$.getJSON('http://api.flickr.com/services/rest/?format=json&method=flickr.photosets.getPhotos&photoset_id=' + photoset_id + '&per_page=1000' + '&page=1' + '&api_key=' + apiKey + '&user_id=' + userId + '&jsoncallback=?', function(data) {
    $.each(data.photoset.photo, function(i, flickrPhoto){
        var basePhotoURL = 'http://farm' + flickrPhoto.farm + '.static.flickr.com/'
        + flickrPhoto.server + '/' + flickrPhoto.id + '_' + flickrPhoto.secret + "_z.jpg";  

        var fullPhotoURL = 'http://farm' + flickrPhoto.farm + '.static.flickr.com/'
        + flickrPhoto.server + '/' + flickrPhoto.id + '_' + flickrPhoto.secret + "_l.jpg";          

        /*var FlickrLink = "http://www.flickr.com/photos/" + data.photoset.owner + "/" + flickrPhoto.id + "/";*/
        $("<img/>").attr("src", basePhotoURL).appendTo("#photographs").wrap(("<div class='item'></div>"))

    });

    **$("<a href='#' class='zoom'/>").appendTo(".item");**
    **$("<a href='#' class='flickr'/>").appendTo(".item");**

    $("#photographs").gridalicious({
        gutter: 2,
        animate: true,
        effect: 'fadeInOnAppear',
        width: 320,
        complete: onComplete()
    });

    function onComplete(){
        $("#loader").delay(20000).fadeOut("slow");
        $(".item").hover(function(){
            $(".zoom, .flickr", this).stop(true,true).fadeIn(300);
        }, function () {
            $(".zoom, .flickr", this).stop(true,true).fadeOut(300);
        });
    }
});
});

.zoom 和 .flickr div 附加到 .item div(其中包含图片)。我需要 var fullPhotoURL和 var FlickrLink因为我想在代码中突出显示(带星号)的锚点中使用它们。当我这样做时,我收到一个错误,即找不到 var fullPhotoURL和 var FlickrLink

我应该如何解决这个问题?

4

2 回答 2

0

您必须在 $.each 块之外定义变量:

$.getJSON('http://api.flickr.com/services/rest/?format=json&method=flickr.photosets.getPhotos&photoset_id=' + photoset_id + '&per_page=1000' + '&page=1' + '&api_key=' + apiKey + '&user_id=' + userId + '&jsoncallback=?', function(data) {

    var fullPhotoURL, FlickrLink;

    $.each(data.photoset.photo, function(i, flickrPhoto){
        var basePhotoURL = 'http://farm' + flickrPhoto.farm + '.static.flickr.com/'
        + flickrPhoto.server + '/' + flickrPhoto.id + '_' + flickrPhoto.secret + "_z.jpg";  

        fullPhotoURL = 'http://farm' + flickrPhoto.farm + '.static.flickr.com/'
        + flickrPhoto.server + '/' + flickrPhoto.id + '_' + flickrPhoto.secret + "_l.jpg";          

        /*FlickrLink = "http://www.flickr.com/photos/" + data.photoset.owner + "/" + flickrPhoto.id + "/";*/
    $("<img/>").attr("src", basePhotoURL).appendTo("#photographs").wrap(("<div class='item'></div>"))

   });

   $("<a href='#' class='zoom'/>").appendTo(".item");
于 2012-12-23T12:01:29.317 回答
0

要在 Javascript 中定义全局变量,您需要跳过var声明项。

var“意味着”本地变量

首先为您的变量分配一个默认值:

fullPhotoURL = null;
FlickrLink = null; //Avoid first upcase char to define variable

然后这些变量将在您的所有函数中可用

于 2012-12-23T12:04:02.920 回答