2015-08-27 33 views
0

我想通過兩個(可能更多的將來)RSS-feeds循環,並將它們放在不同的容器div。我從以下問題開始:JQuery Fetch Multiple RSS feeds通過多個RSS源循環並輸出到不同的div?

這是我的代碼。

var thehtml = ''; 

    $(function() { 
    var urls = ['http://www.gosugamers.net/counterstrike/news/rss', 'http://www.hltv.org/news.rss.php']; 
    for (var i = 0; i < urls.length; i++) { 
     $.ajax({ 
      type: "GET", 
      url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(urls[i]), 
      dataType: 'json', 
      error: function() { 
       alert('Unable to load feed, Incorrect path or invalid feed'); 
      }, 
      success: function (xml) { 
       values = xml.responseData.feed.entries; 
       console.log(values); 

       $.each(values, function(idx, value){ 

        thehtml += '<a class="news-item" href="' + value.link + '" title="' + value.title +'" target="_blank"><p>' + value.publishedDate + '</p><h3>' + value.title + '</h3></a><hr>';  
        }); 

       $("#content_1").html(thehtml); 

      } 
     }); 
    } 
}); 

我加載兩個RSS提要,並在控制檯輸出中,我可以看到兩個數據數組。

現在我使用$(#content_1).html(thehtml);在容器div中輸出HTML格式的訂閱源數據,#content_1

什麼我要做的是把第一個RSS飼料到#content_1第二個到#content_2。我嘗試使用.slice(0,10),但無法使其工作,它似乎不是最好的方式。

+0

將'var thehtml ='';'移出for循環。每次運行for循環時都清空該變量。 – NewToJS

+0

啊!多麼愚蠢......那麼,這就給我留下了一個div中所有的feed數據,我仍然需要一種方法將它們分成兩個不同的容器。 – Lehar001

+0

我會附加新數據而不是覆蓋內部內容。 – NewToJS

回答

3

這是間隔。容器內容將清空以顯示新數據。

更新: Ajax以可選的第二種方法結果爲目標content_1和content_2。

$(function() { 
 
function GetFeeds(){ 
 
var urls = ['http://www.gosugamers.net/counterstrike/news/rss', 'http://www.hltv.org/news.rss.php']; 
 
urls.forEach(function(Query){ 
 
$.ajax({ 
 
    type: "GET", 
 
    url: 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q='+encodeURIComponent(Query), 
 
    dataType: 'json', 
 
    error: function() { 
 
    alert('Unable to load feed, Incorrect path or invalid feed'); 
 
    }, 
 
    success: function(xml) { 
 
//--Target ID's By content_1/2 
 
var Content=parseInt(urls.indexOf(Query))+1; 
 
    \t $("#content_"+Content).html(''); 
 
    $.each(xml.responseData.feed.entries, function(idx, value){ 
 
    $("#content_"+Content).append('<a class="news-item" href="' + value.link + '" title="' + value.title +'" target="_blank"><p>' + value.publishedDate + '</p><h3>' + value.title + '</h3></a><hr>');  
 
    }); 
 
//--------------- 
 
//--Target ID's By Domain (Method Two) 
 
/* 
 
    \t $("#"+Query.split('.')[1]).html(''); 
 
    $.each(xml.responseData.feed.entries, function(idx, value){ 
 
    $("#"+Query.split('.')[1]).append('<a class="news-item" href="' + value.link + '" title="' + value.title +'" target="_blank"><p>' + value.publishedDate + '</p><h3>' + value.title + '</h3></a><hr>');  
 
    }); 
 
-----------------------------------*/ 
 
    } 
 
}); 
 
}); 
 
} 
 
//Call GetFeeds every 5 seconds. 
 
setInterval(GetFeeds,5000); 
 
//Page is ready, get feeds. 
 
GetFeeds(); 
 
});
#content_1{float:left;width:40%;overflow:hidden;border:solid 2px blue;} 
 
#content_2{float:right;width:40%;overflow:hidden;border:solid 2px yellow;} 
 
/* Method Two Styles 
 
#gosugamers{float:left;width:40%;overflow:hidden;border:solid 2px green;} 
 
#hltv{float:right;width:40%;overflow:hidden;border:solid 2px red;} 
 
*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
 
<div id="content_1"></div> 
 
<div id="content_2"></div> 
 
<!-- Method Two Elements 
 
<div id="gosugamers"></div> 
 
<div id="hltv"></div> 
 
-->

如果你不明白任何的源代碼上述請留下下面評論,我添加任何必要的註釋/備註。 鑑賞通過標記答案顯示

我希望這有助於。快樂的編碼!

+0

謝謝你,但我知道如何做到這一點,這不是我的問題的一部分。我的問題是,我如何在#container_1中放置RSS feed 1,並在#container_2中放置RSS feed 2。 我會編輯我的問題,使其更清楚。 – Lehar001

+0

@ user2239595我已經更改了源代碼以完全符合您的要求。我在源代碼「註釋掉」中保留了第二種方法可選方法。如果這回答你的問題,我將不勝感激,如果你可以標記你的問題爲回答。這可以幫助其他具有相同或相似問題的人找到解決方案,並在重疊問題上減少計算器上的重複問題。謝謝。 – NewToJS

+1

美麗!我只是複製+粘貼你的代碼,它創造奇蹟。當我有時間時,我會深入瞭解它。不能投票,但標記爲正確的答案。 – Lehar001

相關問題