0

无法弄清楚为什么我的循环重复了结果?我试图从 kalRss = rss url 获取 RSS 提要。它得到了所有东西,但它以一种奇怪的方式复制了条目。如果您查看萤火虫控制台,您可以发现只有 3 个条目。http://www8.umu.se/project/rut/display/index.html

<script src="https://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
    google.load("feeds", "1");
    var kalRSS = "http://www.upc.umu.se/om-upc/kalendarium/prenumerera/kalender-rss/?categories=concert,conference,courseEducation,culturOnCampus,dissertation,exhibition,lecture,licentiateSeminar,meeting,theOthers,seminar,seminarSeries,workshop&whoCanAttend=employees&calendarIds=69&fromSiteNodeId=45897";
    function initialize() {
        console.log("initialize");
        var container = $('#dailyFeed');
        container.empty();
        //var feed = new google.feeds.Feed("http://fastpshb.appspot.com/feed/1/fastpshb");
        var feed = new google.feeds.Feed("http://www.upc.umu.se/om-upc/kalendarium/prenumerera/kalender-rss/?categories=concert,conference,courseEducation,culturOnCampus,dissertation,exhibition,lecture,licentiateSeminar,meeting,theOthers,seminar,seminarSeries,workshop&whoCanAttend=employees&calendarIds=69&fromSiteNodeId=45897");
        feed.setNumEntries(3);
        feed.load(function(result) {
            if (!result.error) {
                var html = '';
                var entryLenght = result.feed.entries.length;
                console.log("Number of posts: " + entryLenght);
                for (var i = 0; i < entryLenght; i++) {
                    console.log("Loop: " + i);
                    var entry = result.feed.entries[i];
                    console.log(entry);
                    var startTime = entry.content.substr(11, 5)
                    var endTime = entry.content.substr(27, 5)
                    html += '<li class="daily">';
                    html += '<h3 class="georgia">' + entry.title + '</h3>';
                    html += '<h4>' + entry.categories[0] + '</h4>';
                    html += '<p>Info: ' + entry.contentSnippet + '</p>';
                    html += '<p><img src="img/watch.png" class="icon" />' + startTime + ' - ' + endTime + '</p>';
                    html += '</li>';
                    container.append(html);
                }
            }
        });
    }
    google.setOnLoadCallback(initialize);
</script>
4

1 回答 1

1

您正在循环中执行 a html += "<li></li>",但您忘记在循环内重置此变量。因此,在每次迭代中,循环都会将其添加到容器中:

  • 第 1 项
  • 第 1 项,第 2 项
  • 第 1 项、第 2 项、第 3 项

最终结果:

  • 第 1 项,第 1 项,第 2 项,第 1 项,第 2 项,第 3 项

要修复,请在每次迭代时将html变量重置为,例如:""

for (var i = 0; i < entryLenght; i++) {
    var html = '';
    // rest of your code
}
于 2012-12-03T15:01:37.377 回答