我正在尝试使用Javascript(例如jQuery-我们已经导入1.9.1版)从网站的RSS新闻提要中填充5个最新主题的网页菜单,并且我一直在这里拖网内容以查找可行的内容,但最适合我的问题的答案似乎是使用不建议使用的脚本。
RSS提要存储在我们自己的服务器上,因此应确保阅读权限。我更喜欢使用某种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:代码,但是它似乎不起作用?此处代码中的另一个答案给出了一个jQuery代码,此后我不知道如何在HTML中使用它,因此它可能像指导我使用它一样简单?
我使用JS的经验非常有限,因此,我将非常感谢一些非常低技术的建议,以帮助您解决如何在.js文件中包含内容以及如何使其随后显示在HTML中的问题。
新闻提要在这里:链接
谢谢!
根据您找到的答案,我整理了一个jsfiddle进行测试。(请注意,由于无法访问域上的RSS feed,因此我在小提琴中使用了预定义的XML字符串)
这是代码说明
简单的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)
});
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句