2013-04-24 77 views
0

我正在使用此脚本从博客加载新闻,使用Google AJAX Feed API。如何设置Google Ajax FEED Api中的字符数量?下面是我的sript如何设置字符串中的字符数量?

HTML

<div id="feeddiv"> 

</div> 

SCRIPT

<script type="text/javascript" src="https://www.google.com/jsapi"></script> 
<script type="text/javascript"> 
    google.load("feeds", "1") //Load Google Ajax Feed API (version 1) 
</script> 

<script type="text/javascript"> 

    var feedcontainer=document.getElementById("feeddiv") 
    var feedurl="http://anatomicshoes.wordpress.com/feed/" 
    var feedlimit=4 
    var rssoutput="<ul>" 
    function rssfeedsetup(){ 
     var feedpointer=new google.feeds.Feed(feedurl) //Google Feed API method 
     feedpointer.setNumEntries(feedlimit) //Google Feed API method 
     feedpointer.load(displayfeed) //Google Feed API method 
    } 

    function displayfeed(result){ 
     if (!result.error){ 
      var thefeeds=result.feed.entries 
      for (var i=0; i<thefeeds.length; i++){ 

      var pubDate = thefeeds[i].publishedDate; 
      var date = new Date(pubDate); 

      var months = Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"); 
      var string = date.getDate() + "/" + months[date.getMonth()] + "/" + date.getFullYear() 

      rssoutput+="<li><span>•</span> <small>" + string + "</small> - <a href='" + thefeeds[i].link + "' target='_blank'>" + thefeeds[i].title + "</a></li>" 
      } 
      rssoutput+="</ul>" 
      feedcontainer.innerHTML=rssoutput 

     } else 
     alert("Erro ao carregar as notícias!") 
    } 


    window.onload=function(){ 
     rssfeedsetup() 
    } 
    </script> 

这是输出:

• 18/04/2013 - Vincent Ko features the Jardins… 
• 12/04/2013 - Stay stylish this Spring… 
• 10/04/2013 - Coming soon…. 
• 5/04/2013 - Introducing the Jardins – A development in Ethical footwear! 

但我想是这样的(不是整个标题):

• 18/04/2013 - Vincent Ko features the... 
• 12/04/2013 - Stay stylish this Spring... 
• 10/04/2013 - Coming soon... 
• 5/04/2013 - Introducing the Jardins... 

回答

0

你可以做这样的事情......(不是很优雅):

<script type="text/javascript" src="https://www.google.com/jsapi"></script> 
<script type="text/javascript"> 
    google.load("feeds", "1") //Load Google Ajax Feed API (version 1) 
</script> 

<script type="text/javascript"> 

    function shortenString(str){ 
     var limit = 23; 
     if(str.length > limit) 
      str = str.substring(0, limit) + '...'; 
     return str; 
    } 

    var feedcontainer=document.getElementById("feeddiv") 
    var feedurl="http://anatomicshoes.wordpress.com/feed/" 
    var feedlimit=4 
    var rssoutput="<ul>" 
    function rssfeedsetup(){ 
     var feedpointer=new google.feeds.Feed(feedurl) //Google Feed API method 
     feedpointer.setNumEntries(feedlimit) //Google Feed API method 
     feedpointer.load(displayfeed) //Google Feed API method 
    } 

    function displayfeed(result){ 
     if (!result.error){ 
      var thefeeds=result.feed.entries 
      for (var i=0; i<thefeeds.length; i++){ 

      var pubDate = thefeeds[i].publishedDate; 
      var date = new Date(pubDate); 

      var months = Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"); 
      var string = date.getDate() + "/" + months[date.getMonth()] + "/" + date.getFullYear() 

      rssoutput+="<li><span>•</span> <small>" + string + "</small> - <a href='" + thefeeds[i].link + "' target='_blank'>" + shortenString(thefeeds[i].title) + "</a></li>" 
      } 
      rssoutput+="</ul>" 
      feedcontainer.innerHTML=rssoutput 

     } else 
     alert("Erro ao carregar as notícias!") 
    } 


    window.onload=function(){ 
     rssfeedsetup() 
    } 
    </script> 

DEMO: http://jsfiddle.net/dirtyd77/QRBUP/2/

+0

完美的作品..不优雅,但功能!谢谢! – Preston 2013-04-24 18:29:56

+0

@Preston肯定的事情!需要帮助请叫我!也可以自由地检查这个被接受的答案。 – Dom 2013-04-24 18:31:26

+0

当然是!我忘了检查...!Cya .. – Preston 2013-04-24 18:57:46