0

我在这里使用 jQuery 提要脚本http://camagu.github.io/jquery-feeds/demos/:下面是一个带有 jquery 的工作 html 页面,它从 html 列表中的链接列表中提取指向 RSS 提要的链接和将它们传递给 feed 函数,该函数将它们显示在页面上的 div 中。

我的问题是我想动态创建 url 数组以从页面 itel 上的内容(链接列表)(我已经完成)传递给提要函数,而不是一次传递一个链接列表 [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>
4

1 回答 1

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 个项目传递给您的 feeds() 方法,而是传递我们之前构建的对象

$('.feed').feeds( {
   feeds: listoflinks, 
  // your function (didn't modify this)
  preprocess: function(feed) {}
 });
于 2013-10-21T06:35:08.837 回答