我在这里使用jQuery供稿脚本http://camagu.github.io/jquery-feeds/demos/: 下面是一个带有jquery的工作html页面,它从html列表中的链接列表中提取到RSS源的链接,并将它们传递给feeds函数,该函数在页面上的div中显示它们。
我的问题是我想动态创建网址数组,以便从页面上的内容传递给feed函数(listoflinks)(我已经完成了),但不是一次传递一个listoflinks [ 0],我想在listoflinks中传递整个数组。如何修改它以使用任意数量的链接而不是指定的数组大小,我知道我应该做一个foreach循环,但不知道它应该放在哪里才能工作。目前,每个链接都通过$ listoflinks [0],$ listoflinks [1],$ listoflinks [2]等传递给feed函数。
<!DOCTYPE html>
<head>
<!-- Include jQuery -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!-- Load moment.js, used to format dates, totally optional -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/1.6.2/moment.min.js" charset="utf-8"></script>
<!-- Load jQuery Feeds -->
<script src="js/jquery.feeds.min.js" charset="utf-8"></script>
<script type="text/javascript">
// A $( document ).ready() block.
$(document).ready(function() {
console.log( "ready!" );
var listoflinks = [];
$(".field-name-field-news ul li a").each(function() { listoflinks.push("http:" + $(this).attr("href")) });
//console.log(listoflinks[0]);
$('.feed').feeds({
feeds : {
// uniquename: "https://www.domain.com/rss",
option0: listoflinks[0],
option1 : listoflinks[1],
option2 : listoflinks[2],
option3 : listoflinks[3],
},
preprocess : function(feed) {
// Using moment.js to diplay dates as time ago
this.publishedDate = moment(this.publishedDate).fromNow();
},
entryTemplate : 'entryTmpl',
// onComplete : function(entries) {
// $(this).find('a').attr('target', '_blank');
// }
});
});
</script>
</head>
<body>
<script type="text/html" id="entryTmpl">
<article class="<!=source!> entry">
<p>
<a class="link" href="<!=feedLink!>" title="<!=feedDescription!>"> <!=feedTitle!></a>
<span class="publishedDate"><!=publishedDate!></span>
</p>
<p class="author"><!=author!></p>
<div class="content"><!=content!></div>
<div class="mediagroup">mediaGroup: <!=mediaGroup!></div>
<div class="categories">categories: <!=categories!></div>
<div class="feedauthor">feedAuthor<!=feedAuthor!></div>
</article>
</script>
<div class="field field-name-field-news field-type-link-field field-label-hidden">
<div class="field-items">
<div class="field-item even"><div class="item-list">
<ul><li class="first"><a href="//www.filmschoolrejects.com/feed">Filmmaking Tips | Film School Rejects</a></li>
<li><a href="//www.filmmakingstuff.com/feed/">Filmmaking Stuff | For Independent Filmmakers</a></li>
<li><a href="//www.reddit.com/r/Filmmakers/.rss">Filmmakers - Reddit</a></li>
<li><a href="//feeds.feedburner.com/povdocs">For Filmmakers: Documentary Filmmaking Resources from POV - PBS</a></li>
<li><a href="//filmmakermagazine.com/category/filmmaking/feed/">Filmmaking | Filmmaker Magazine</a></li>
<li><a href="//cheapfilmmaking.com/feed">Cheap Filmmaking</a></li>
<li><a href="//www.filmmakingreview.com/feed/">Filmmaking Review</a></li>
<li><a href="//www.filmindependent.org/feed/">Artist Development | Film Independent</a></li>
<li class="last"><a href="//www.allaboutindiefilmmaking.com/feeds/posts/default">All About Indie Filmmaking</a></li>
</ul></div></div>
</div>
</div>
</div>
<div id="feed" class="feed">
Feed content will go in here.
</div>
</body>
</html>
答案 0 :(得分:2)
我建议你创建一个空对象
var listoflinks = {};
然后替换此行
$(".field-name-field-news ul li a").each(function() { listoflinks.push("http:" + $(this).attr("href")) });
使用以下内容向对象添加项目
$(".field-name-field-news ul li a").each(function(index) {
listoflinks['option'+index] = "http:" + $(this).attr("href");
});
现在,不要将4个项目传递给feed()方法,而是传递我们之前构建的对象
$('.feed').feeds( {
feeds: listoflinks,
// your function (didn't modify this)
preprocess: function(feed) {}
});