在javascript中从RSS结果创建动态链接

时间:2014-07-03 21:03:53

标签: javascript rss

我使用Google Feed API将RSS Feed调用到我网站上的条件div中,并希望将每个Feed项的网址作为链接返回,但我没有运气。 Feed正在返回并根据请求显示正常,但我似乎无法找出createTextNode(entry.link)。

即使朝着正确的方向努力也会很棒。我愿意找到答案,但我似乎没有正确搜索......

代码:

google.load("feeds", "1");

// Callback function, for when a feed is loaded.
function feedLoaded(result) {
  if (!result.error) {
    // Grab the container we will put the results into
    var container = document.getElementById("announcements");
    container.innerHTML = '';

    // Loop through the feeds, putting the titles onto the page.
    // Check out the result object for a list of properties returned in each entry.
    // http://code.google.com/apis/ajaxfeeds/documentation/reference.html#JSON
    for (var i = 0; i < result.feed.entries.length; i++) {
      var entry = result.feed.entries[i];
      var div = document.createElement("div");
      var entryStr = entry.title.indexOf('');

      // Formatting box for feed
      var span = document.createElement("span");
      span.className = "subhead";

      // Spacing elements
      var lineBreak = document.createElement("br");
      var paraBreak = document.createElement("p");

      // Conditional statement; if the returned entry contains '', run append statements
      if(entryStr != -1) {
        div.appendChild(document.createTextNode(entry.publishedDate));
        div.appendChild(paraBreak);
        div.appendChild(document.createTextNode(entry.title));
        div.appendChild(paraBreak);
        div.appendChild(document.createTextNode(entry.link));
        container.appendChild(span);
        span.appendChild(div);
      }
    }
  }
}

function OnLoad() {
  // Create a feed instance that will grab the appropriate feed.
  var feed = new google.feeds.Feed("http://*feedaddress*");
  feed.setNumEntries(4);
  // Calling load sends the request off.  It requires a callback function.
  feed.load(feedLoaded);
}

google.setOnLoadCallback(OnLoad);

1 个答案:

答案 0 :(得分:0)

好吧我明白了。我做了以下(请参阅//链接创建,附加:

google.load("feeds", "1");

// Callback function, for when a feed is loaded.
function feedLoaded(result) {
  if (!result.error) {
    // Grab the container we will put the results into
    var container = document.getElementById("announcements");
    container.innerHTML = '';

    // Loop through the feeds, putting the titles onto the page.
    for (var i = 0; i < result.feed.entries.length; i++) {
      var entry = result.feed.entries[i];
      var div = document.createElement("div");
      var date = new Date(entry.publishedDate);
      var months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
      var newDate =  months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
      var entryStr = entry.title.indexOf('');

      // Formatting box for feed
      var span = document.createElement("span");
      span.className = "subhead";

      // Spacing elements
      var lineBreak = document.createElement("br");
      var paraBreak = document.createElement("p");
      var parbBreak = document.createElement("p");

      // Link creation
      a = document.createElement('a');
      a.href = (entry.link);
      a.innerHTML = " Read More...";


      // Conditional statement; if the returned entry contains '', run append statements
      if(entryStr != -1) {
        div.appendChild(document.createTextNode(newDate));
        div.appendChild(paraBreak);
        div.appendChild(document.createTextNode(entry.title));
        div.appendChild(a);
        container.appendChild(span);
        span.appendChild(div);
      }
    }
  }
}

function OnLoad() {
  // Create a feed instance that will grab the feed.
  var feed = new google.feeds.Feed("my site");
  feed.setNumEntries(5);
  // Calling load sends the request off.  It requires a callback function.
  feed.load(feedLoaded);
}

google.setOnLoadCallback(OnLoad);

现在唯一的问题是让链接在新窗口中打开。我想这是一个新问题!