使用JSON变量作为公共变量?

时间:2012-12-23 11:54:33

标签: jquery json flickr

我正在将我的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&amp; .flickr div附加到.item div(包含图片)。我需要var fullPhotoURL 和var FlickrLink ,因为我想在代码中突出显示(带星号)的锚点中使用它们。当我这样做时,我得到一个错误,无法找到var fullPhotoURL 和var FlickrLink

我该如何解决这个问题?

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分:0)

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

var“表示”lacal变量

首先为变量指定一个默认值:

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

然后,所有功能都可以使用这些变量