我正在尝试创建一个聚合器,它将从RSS中提取项目并将它们存储在jQuery数组中(这样我就可以遍历数组并随机输出)。
我已经找到了一些关于使用jQuery.parseXML的信息,但是,如果我不能弄清楚,因为我在这个领域没有太多的知识,而且演示没有显示到RSS的链接是在哪里添加的?
<p id="someElement"></p>
<p id="anotherElement"></p>
<script>
var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$title = $xml.find( "title" );
// Append "RSS Title" to #someElement
$( "#someElement" ).append( $title.text() );
// Change the title to "XML Title"
$title.text( "XML Title" );
// Append "XML Title" to #anotherElement
$( "#anotherElement" ).append( $title.text() );
</script>我应该在哪里添加RSS提要链接?阵列是在哪里制作的?
最终,这将是一个马赛克墙,所有项目的视觉输出HTML。
我从RSS提要中获得的每个项目的数据如下:
我需要提取所有这些数据,以便输出标题、描述、将两者包装在链接标签中,然后存储类别并使用存储的发布日期随机输出(例如,选择最后30天并以随机顺序输出这些项目)。
这是我使用Ajax页面编写的代码,但我不知道这是否正确,或者它应该如何工作……
jquery.ajax(http://www.sagittarius-digital.com/news.rss [, dataType xml])发布于 2014-01-02 15:40:17
当您通过ajax使用'xml‘调用rss时,不需要使用$.parseXml()。
$.ajax({
url:'news.rss',
dataType:'xml',
success:function( xmlString ){
var $xml = $( xmlDoc ),
$title = $xml.find( "title" );
$title.each(function(){
console.log($(this).text());
});
}
});请记住,RSS url必须位于javascript文件所在的同一个域中。如果不是,那么您将需要做一些服务器端逻辑,例如cURL,以获得rss提要。
发布于 2014-01-02 17:00:18
试着做些像
jQuery(function(){
$.ajax({
url: 'news.rss', //place the your rss feed url here
dataType: 'xml'
}).done(function(xml){
var items = $(xml).find('item').map(function(){
var $item = $(this);
var array = ['<li>'];
array.push('<a href="' + $.trim($item.find('link').text()) + '">')
array.push('<h3>' + $item.find('title').text() + '</h3>')
array.push('</a>');
array.push('<p>' + $item.find('description').text() + '</p>');
array.push('<span class="category">' + $item.find('category').text() + '</span>')
array.push('<span class="pub-date">' + $item.find('pubDate').text() + '</span>')
array.push('</li>');
return array
}).get();
$('ul').append(items.join(' '));
}).fail(function(){
conole.log('error', arguments)
})
})演示:柱塞
https://stackoverflow.com/questions/20885942
复制相似问题