簡體   English   中英

如何使用Google Feed API從RSS Feed加載圖像?

[英]How to Load Images from a RSS Feed using Google Feed API?

我無法使用Google Feed API加載圖片,而是使用mediaGroup加載圖片。

這是我為獲取提要而編寫的javascript代碼

<script type="text/javascript">

google.load("feeds", "1");
function initialize() 
   {
    var feed = new google.feeds.Feed("http://techzei.com/feed");
    feed.setNumEntries(14);
    feed.load(function (result) {
        if (!result.error) 
        {
            result.feed.entries.forEach(function(entry)
            {
              $('#feed').append('<div class="card"><a href="' + entry.link + '">' + entry.title + '</a><p>' + entry.contentSnippet + '</p><br><p>'+entry.publishedDate+'"<img src="'+ entry.mediaGroups+'"/></div>');
            });
        }
    });
}
google.setOnLoadCallback(initialize);

HTML

<article id="feed"></article>

我現在得到的所有圖像都是“未定義”的。 我應該做別的事情嗎? Google Feed API開發人員對此沒有太多關注

該供稿似乎沒有任何mediaGroup條目,這就是您在結果中未定義的原因,但是您可以使用jQuery從entry.content提取圖像。

將圖片網址放入數組,並顯示第一個找到的圖片(或不顯示):

var content = document.createElement("content");
content.innerHTML = entry.content;                   
var images = $(content).find('img').map(function(){
    return $(this).attr('src')
}).get();
$('#feed').append('<div class="card"><a href="' + entry.link + '">' +
    entry.title + '</a><p>' + entry.contentSnippet + '</p><br><p>'+ 
    entry.publishedDate +'</p>' + 
    (images.length == 0 ? '' :'<img src="'+ images[0] +'"/>') + '</div>');

通過將img標簽附加到變量來顯示所有圖像:

var content = document.createElement("content");
content.innerHTML = entry.content;
var images = "";
$(content).find('img').each(function() {  
    images += this.outerHTML;
}); 
$('#feed').append('<div class="card"><a href="' + entry.link + '">' +
    entry.title + '</a><p>' + entry.contentSnippet + '</p><br><p>' +
    entry.publishedDate + '</p>' + images +'</div>');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM