【问题标题】:JQuery loop through output buffer using .each()JQuery 使用 .each() 循环输出缓冲区
【发布时间】:2013-03-01 16:50:54
【问题描述】:

好的,我有一个函数可以获取 vine 推文,然后通过 iFrame 显示视频。这很好用,唯一的问题是我不想一次列出它们,我只想显示一个,然后在完成后显示下一个。这可能很简单,但我一直在看代码很长时间,现在我想不通。请帮忙。

         function getTweets() {
                    var url='http://search.twitter.com/search.json?callback=?&q=givingvine';
                    $.getJSON(url,function(json){

                    //setup an array to buffer output
                    var output = [];

                    //a for loop will perform faster when setup like this
                    for (var i = 0, len = json.results.length; i < len; i++) {
                   //instead of appending each result, add each to the buffer array
                   output.push('<p>' + '<iframe class="video" src="' + json.results[i].text.substr(json.results[i].text.indexOf("http")) +'" frameborder="0" width="1080" height="800" style="margin:-155px -100px -130px -60px;"></iframe>' + '</p>');

                    }

                //now select the #results element only once and append all the output at once, then slide it into view
                  $.each(output, function (intIndex, objValue) {
                    $("#results").html(objValue);
                    });
                });
                }
                 //set an interval to run the getTweets function (30,000 ms is 5 minutes), you can cancel the interval by calling clearInterval(timer);
                 var timer = setInterval(getTweets, 10000);
                clearInterval(timer);
                //run the getTweets function on document.ready
                getTweets();

            });

【问题讨论】:

    标签: jquery loops for-loop


    【解决方案1】:

    如果我理解正确,您希望将结果存储在输出中,然后只将第一个结果附加到#results。那么当用户做某事时添加另一个?

    我建议您将当前“位置”存储在一个变量中,然后当用户执行您想要添加下一个的操作时,将其增加 1

    类似

      var output =[],currentKey =0;
      function getTweets() {
                    var url='http://search.twitter.com/search.json?callback=?&q=vine';
                    $.getJSON(url,function(json){
    
                    //setup an array to buffer output
    
    
                    //a for loop will perform faster when setup like this
                    for (var i = 0, len = json.results.length; i < len; i++) {
                   //instead of appending each result, add each to the buffer array
                   output.push('<p>' + '<iframe class="video" src="' + json.results[i].text.substr(json.results[i].text.indexOf("http")) +'" frameborder="0" width="1080" height="800" style="margin:-155px -100px -130px -60px;"></iframe>' + '</p>');
    
                    }
    
                //now select the #results element only once and append all the output at once, then slide it into view
                    $("#results").html(output[currentKey]);
                });
                }
    
             getTweets();
    
            $('#next').click(function(){
              currentKey +=1;
              if(currentKey  < output.length){
             $('#results').append(output[currentKey]);   
           }
           });
    

    上面的示例假设您有某种带有 id=next 的按钮,单击该按钮会将下一项附加到结果中,例如

      <button id="next">Next Video</button>
    

    如果要替换当前视频,请使用

            $('#results').html(output[currentKey]);   
           //  instead of
           $('#results').append(output[currentKey]);   
    

    【讨论】:

    • 这有点像我要找的东西,我不想要一个按钮,我希望它在特定间隔后切换
    • 然后将“click”函数替换为 window.setInterval(function(){ //上面的代码在 click 函数中 }, 10000); //10 秒间隔应该存储在一个变量中,这样你就可以在达到项目长度时杀死它
    【解决方案2】:

    编辑:

    听起来你想要这样的东西:

    output.push('<p>' + '<iframe id="iframe' + i.toString() + '"  class="video" src="' + json.results[i].text.substr(json.results[i].text.indexOf("http")) +'" frameborder="0" width="1080" height="800" style="margin:-155px -100px -130px -60px;"></iframe>' + '</p>');
    
                        }
    

    然后在这里:

                  $.each(output, function (intIndex, objValue) {
                    $("#results").html(objValue);
                    //Some Timer code here;
                    //Remove previous iframe, add a new iframe
                    $('#results').empty();
                    });
    

    您也可以更改 forloop 以使用 src 填充输出,然后在 foreach 中执行以下操作:

                  $.each(output, function (intIndex, objValue) {
                    $("#iframe").attr('src', objValue)
                    //Some Timer code here;
                    });
    

    如果您想在设置中一次显示一个 iframe:

    您可能想要使用 each() 函数:

    $.each(output, function (intIndex, objValue) {
         $("#results").html(objValue);
    });
    

    代替:

    $("#results").html(output);
    

    【讨论】:

    • 是的,这与我想要的很接近。我要添加什么来使 iframe 切换到数组中的下一个?
    • 为此,您不需要选择结果并应用 html,而是需要选择 iframe - 通过其类或向标签添加 ID 属性。所以像 $('iframe').html(objValue);
    • 我对 JQuery 真的不是很好,我如何在 for 循环中生成 iframe 来做到这一点?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多