如何设置字符串中的字符数?

时间:2013-04-24 17:52:39

标签: javascript string google-feed-api

我正在使用此脚本使用Google AJAX Feed API从博客加载新闻。如何从Google Ajax FEED Api设置tittle的字符数?下面是我的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...

1 个答案:

答案 0 :(得分: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>

样本: http://jsfiddle.net/dirtyd77/QRBUP/2/