【问题标题】:jQuery-HTML: Display Two multiple feeds into onejQuery-HTML:将两个多个提要合二为一
【发布时间】:2016-02-08 12:19:04
【问题描述】:

所以,我试图合并两个提要,并将其显示在按时间排序的两列中,从左上角的最新开始,右下角最旧的开始。准确地说,这是我想要的顺序:

------------------------
Newest     | 4th Newest
2nd Newest | 5th Newest
3rd Newest | 6th Newest 
------------------------

问题是,下面的代码不起作用,返回错误

Uncaught TypeError: item is not a function  (index:329)

我使用的代码如下。

<script>
  $(document).ready(function() {
    var blogFeed = $.ajax({
      type: "GET",
      url: "http://www.foxinflame.tk/blog/feed",
      dataType: "xml"
    })
    var videoFeed = $.ajax({
      type: "GET",
      url: "/videoFeed.php",
      dataType: "xml"
    })
    $.when(blogFeed, videoFeed).done(function(blogXML, videoXML){
//The one below this one is the 329 line
      var combinedXML = $(blogXML).find("item").concat(videoXML.find("entry"));
      var sortedXML = combinedXML.sort(function(videoFeedCompare, blogFeedCompare){
        var videoFeedDate = Date.parse(videoFeedCompare.find("published"));
        var blogFeedDate = Date.parse(blogFeedCompare.find("pubDate"));
        return videoFeedDate - blogFeedDate;
      });
      console.log(sortedXML.item(0));
      sortedXML.forEach(function(eachCounter) {
        console.log("stuff");
        var title = $(this).find("title").text();
        console.log(title);
        var description = $(this).find("description").text();
        var comments = +($(this).find("slash:comments").text());
        var pubDate = $(this).find("pubDate").text();
        var link = $(this).find("link").text();
        if(eachCounter < 3){
                  $("#firstColumnOfItems").append("<div class='postCollection'><div class='z-depth-1 blogpost' style='min-height: 300px'><br><h5><a style='color:black' href='"+link+"'>"+title+"</a></h5><br><p>"+description+"<br><i>"+comments+" Comments. Published at "+pubDate+"</i></p></div></div>");
                } else if(eachCounter < 6) {
                  $("#secondColumnOfItems").append("<div class='postCollection'><div class='z-depth-1 blogpost' style='min-height: 300px'><br><h5><a style='color:black' href='"+link+"'>"+title+"</a></h5><p>"+description+"<br><i>"+comments+" Comments. Published at "+pubDate+"</i></p></div></div>");
                }
      });
    });
  })
</script>

请注意,VideoFeed.php 只是执行file_get_contents 并回显它,因为 JS 无法执行此操作(不存在访问控制允许原始标头)。

出了什么问题,我该如何解决?

【问题讨论】:

    标签: javascript jquery html feed


    【解决方案1】:

    将console.log(sortedXML.item(0)); 更改为console.log(sortedXML.item[0]);

    【讨论】:

    • 现在它返回“条目不是函数”。在与Concat() 函数的行。它也不会将该项目回显到控制台。
    • 您必须查看$(blogXML).find("item") 是否正在退货。无论如何,我认为 .concatdon 不适用于 jquery 元素数组。也许你可以试试别的,比如.push
    • 看起来combinedXML 不是一个数组。 Foreach 函数对它不起作用,并且在使用combinedXML === Array 检查时它不会返回 true。是的,我确实使用了 Push。
    • 创建一个 jsfiddle,也许我可以帮助你。只看代码很难分辨
    • 那是真的。但是,由于我的代码使用来自其他网站的提要,“No Access Control Allow Origin Header”会阻止代码在 JSFiddle 上运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 2019-05-26
    • 1970-01-01
    相关资源
    最近更新 更多