javascript - 使用 Javascript 将 RSS 标题解析为 HTML

标签 javascript jquery html xml rss

我正在尝试使用 Javascript(例如 jQuery - 我们已经导入 v. 1.9.1)使用我们网站的 RSS 新闻源中的 5 个最新主题填充网页上的菜单,并且我一直在此处搜寻内容以查找可行的东西,但最适合我的问题的答案似乎是使用已弃用的脚本。

RSS feed 存储在我们自己的服务器上,因此应确保阅读权限。我更喜欢使用某种 Javascript,我只需要有标题,例如最新的 X 新闻及其链接被扔进

<li><a href="Link to article">first news header</a></li>
<li><a href="Link to article">second news header</a></li>

这里的答案之一让我找到了这个 JSfiddle:Code但似乎不起作用?另一个答案在这里 code给出了一段 jQuery 代码,我不知道如何在 HTML 中使用它,所以它可能就像指导我使用它一样简单?

我使用 JS 的经验非常有限,所以我非常感谢一些技术含量很低的建议,告诉我如何让 .js 文件中包含的内容以及之后如何使其出现在我的 HTML 中。 .

新闻源在这里:Link

谢谢!

最佳答案

基于answer你也发现了,我整理了一个jsfiddle用于测试目的。 (请注意,我在 fiddle 中使用预定义的 XML 字符串,因为我无法访问域上的 RSS 提要)

这里是代码解释

简单的 HTML:

<ul id="feed">
</ul>

Javascript:

$(document).ready(function(){

var x=10; // your X iteration limit

// load the xml data. it is parsed by jquery
$.get("http://www.flatpanels.dk/rss/nyhed.xml", function(data) {
    var $xml = $(data);

    $xml.find("item").each(function(i, val) { // find the items in the rss and loop

        // create an item object with all the necessary information out of the xml
        var $this = $(this),
            item = {
                title: $this.find("title").text(),
                link: $this.find("link").text(),
                description: $this.find("description").text(),
                pubDate: $this.find("pubDate").text(),
                author: $this.find("author").text(),
                guid: $this.find("guid").text()
        };
        // replace the CDATA in the item title
        item.title = item.title.replace("<![CDATA[", "").replace("]]>", "");

        // #feed selects the ul element with the id feed
        // and append() appends a newly created li element
        // to the ul
        $('#feed').append($('<li><a href="' +item.guid +'">' +item.title +'</a></li>'));

        return i<(x-1); // (stop after x iterations)
    });
});
});

关于javascript - 使用 Javascript 将 RSS 标题解析为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29818858/

相关文章:

javascript - 如何跟踪 2 个嵌套的 ng-repeats 中的累积索引

jquery - 如何使文本框只允许三位小数点后的数字?

jquery - 如何使用 Modelform 和 jquery 在 django 中获取相互依赖的下拉菜单?

html - svg 在 firefox 和 IE 中的问题

javascript - 用变量预填充 HTML 值?

javascript - Angular.JS - 事件被多次触发

javascript - 如何从 Bootstrap 轮播中删除白色背景?

javascript - 如何更改 jquery DateTimePicker 中的年份范围?

css - 如何让页脚停留在网页底部?

javascript - 我什么时候需要转义 metacharectars? (jQuery 选择器)