【问题标题】:JavaScript loop pulling in the same image from Instagram's APIJavaScript 循环从 Instagram 的 API 中提取相同的图像
【发布时间】:2014-11-02 03:34:51
【问题描述】:

我正在使用 Instagram 的 API 来显示所有带有特定主题标签的近期照片。我有一个成功显示所有标签的横幅,并将其用作横幅的背景。我想覆盖标签中的 5 张最近照片。下面的代码是我目前所拥有的,但不幸的是循环显示了 5 个相同的图像。

这是我目前所拥有的图像:

$(function() {
    $.ajax({
        type: "GET",
        dataType: "jsonp",
        cache: true,
        url: "https://api.instagram.com/v1/tags/gymspotter/media/recent?access_token=xxxxxxxxxxxxxxxxxxxxxxxxx",
        success: function(data) {
            var tagged_photos = [];
            $.each(data.data, function(i, tag){
              tagged_photo = tag.images.thumbnail.url;
              $("#instagram-tagged-showcase").append('<img src="'+tagged_photo+'">')
              if(i < 5) { tagged_photos[i] = tag.images.thumbnail.url; }
            });

            $.each(tagged_photos, function(i, tag){
              $("#instagram-tagged-feed").append('<li class="instagram-tagged-li"><img src="'+tagged_photos+'" class="instagram-tagged-photo"></li>');
            });
        }
    });
});

【问题讨论】:

  • 在你的第二个不应该是:tagged_photos[i] ?
  • 喜欢这个? $.each(tagged_photos[i], function(i, tag){ - 似乎不起作用
  • 你的第二个each 循环了tagged_photos,但它在函数内部既不使用i 也不使用tag;它只是重复使用tagged_photos。如果您不使用循环变量,则不清楚您的循环在做什么。
  • 不,&lt;img src="'+tagged_photos[i]+'"
  • 完美运行的伴侣。非常感谢。

标签: javascript loops each instagram


【解决方案1】:

在第二个循环中使用+tag+ 而不是+tagged_photos+

$(function() {
    $.ajax({
        type: "GET",
        dataType: "jsonp",
        cache: true,
        url: "https://api.instagram.com/v1/tags/gymspotter/media/recent?access_token=xxxxxxxxxxxxxxxxxxxxxxxxx",
        success: function(data) {
            var tagged_photos = [];
            $.each(data.data, function(i, tag){
              tagged_photo = tag.images.thumbnail.url;
              $("#instagram-tagged-showcase").append('<img src="'+tagged_photo+'">')
              if(i < 5) { tagged_photos[i] = tag.images.thumbnail.url; }
            });

            $.each(tagged_photos, function(i, tag){
              $("#instagram-tagged-feed").append('<li class="instagram-tagged-li"><img src="'+tag+'" class="instagram-tagged-photo"></li>');
            });
        }
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2022-07-25
    相关资源
    最近更新 更多