在div中加载RSS链接

时间:2013-12-19 08:01:59

标签: javascript jquery ajax xml

希望你们能够帮助我解决这个(可能很容易)的问题。

我有以下HTML

<div id="rssfeed"></div>

JS包括FeedEK plugin

$(document).ready(function () {
    $('#rssfeed').FeedEk({
        FeedUrl: 'http://www.essent.nl/content/system/xml/rss_nieuws.rss',
        MaxCount: 5,
        ShowDesc : false
    });
});

RSS-feed完美显示。链接在新窗口中默认打开,我不想这样做。

我的问题是如何让RSS项目的内容加载到同一个div。 例如,当我单击链接时,该RSS项的内容应加载到#rssfeed内。 应该会出现一个后退按钮以返回到RSS源。

我知道AJAX应该在这里将HTML内容解析为#rssfeed。但是我不知道如何用AJAX附加RSS项的URL。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我可能有一个外部脚本将RSS源解析为json,然后你可以使用ajax将其加载到yr RSS div中。这样您就可以更好地控制布局和内容。

这应该是评论,但我还不能发布它们!对不起,当我不在手机上时,我会尝试在这里发布一些示例代码:

编辑:

好的 - 这是一个不完整的答案,但希望能指出你正确的方向。

用于解析rss的PHP示例:

<?php
header('Content-type: application/json');

$feed = file_get_contents('http://www.essent.nl/content/system/xml/rss_nieuws.rss');
$xml = new SimpleXmlElement($feed);

foreach($xml->channel->item as $i){
    echo json_encode($i); 
}
?>

您需要创建自己的数据结构,而不是仅仅将其回显到屏幕上。使输出可用:

示例输出

{
    "title": "this is a title",
    "description": "this is the description",
    "link": "this is the link to the content"
},
{
    "title": "this is another title",
    "description": "this is another description",
    "link": "this is another link to the content"
},
{
    "title": "etc",
    "description": "etc",
    "link": "etc"
}

现在在您的javascript中加载您想要的内容:

<script>
    $.get('myphpoutput.php', function(){
        //do stuff with the json for display
    })
 </script>   

使用这种方法,您可以在php和javascript阶段完全控制内容。希望这有帮助!