0

我正在尝试遍历 instagram api 中的数组。该数组是标签,之后我希望每个标签附加到我的 html 中的一个 li 元素。我已经尝试了几种方法,例如 for 循环中的 for 循环。现在我已经将它设置为数组标签中的第一个元素显示,但我希望每个标签都获得它自己的 li 标签,我需要遍历名为标签的数组还是什么?谢谢!这是我的代码:

//instagram api example, this code makes you fetch images after searching specific hashtag
$(document).ready(function () {
    var myId = "---";
    //sets so form doesnt override jquery
    $('#submit').click(function (event) {
        event.preventDefault();
    })
    $('#submit').click(searchInst);
    //shows a loading bar when press the btn
    // Setup the url for the API
    //var url = "https://api.instagram.com/v1/tags/"+input+"/media/recent?client_id=dff465e44a4d4026b554b3d7925643cc&callback=?&count=100";

    // Tell jQuery to fetch the data.
    // When it returns the data, it will call our `processImages` function 
    //$.getJSON(url, processImages);
    function searchInst() {
        $('.feed').empty()
        $('#load').append('<p>The images are loading..</p><br><img src="img/load.gif">')
        var input = $("#textInput").val()
        var url = 'https://api.instagram.com/v1/tags/' + input + '/media/recent?client_id=' + myId + '&callback=?&count=100'
        $.getJSON(url, processImages);
    };

    function processImages(chilibiff) {
        // The variable f represents the information we got back.
        var i = 0;
        for (var i = 0; i < chilibiff.data.length; i++) {
            var f = chilibiff.data[i]
            var imgSrc = f.images.standard_resolution.url
            var userImg = f.user.profile_picture
            var userName = f.user.username
            //tag for loop doesn't work, try code for in on your server
            var tags = f.tags[0]
            $('.feed').append('<img src="' + userImg + '" id="profile"><p>' + userName + '</p>')
            $('.feed').append('<img src="' + imgSrc + '">')
            $('.feed').append('<li>' + tags + '</li>')
        }
        $('#load').empty()
    }
});
4

1 回答 1

0

好的,所以这将解决问题。循环结果后需要循环遍历数组。

    //instagram api example, this code makes you fetch images after searching specific hashtag

    $(document).ready(function() {

    var myId = "------";

    //sets so form doesnt override jquery
    $('#submit').click(function(event) {
event.preventDefault();
})

      $('#submit').click(searchInst);

      //shows a loading bar when press the btn

     // Setup the url for the API
    //var url = "https://api.instagram.com/v1/tags/"+input+"/media/recent?   client_id=dff465e44a4d4026b554b3d7925643cc&callback=?&count=100";

    // Tell jQuery to fetch the data.
    // When it returns the data, it will call our `processImages` function 
    //$.getJSON(url, processImages);

    function searchInst() {
$('.feed').empty()
$('#load').append('<p>The images are loading..</p><br><img src="img/load.gif">')
var input = $("#textInput").val()
var url = 'https://api.instagram.com/v1/tags/' + input + '/media/recent?client_id=' +    myId + '&callback=?&count=100'
$.getJSON(url, processImages);
    };

    function processImages(chilibiff) {
    // The variable f represents the information we got back.
    for (var i = 0; i < chilibiff.data.length; i++) {
    var f = chilibiff.data[i]
    var imgSrc = f.images.standard_resolution.url
    var userImg = f.user.profile_picture
    var userName = f.user.username
    //tag for loop doesn't work, try code for in on your server
    $('.feed').append('<img src="' + imgSrc + '">')
    $('.feed').append('<img src="' + userImg + '" id="profile">' + '<p><a   href="http://www.instagram.com/' + userName + '" target="_blank">' + userName + '</a></p>')

  for (var x=0; x < f.tags.length; x++) {
  var tag = f.tags[x]
  $('.feed').append('<li>' + tag + '</li>')
    }

    }

    $('#load').empty()
    }


    });
于 2013-10-09T20:27:06.407 回答