2013-03-22 33 views
0

我想從tumblr博客拉,並使用JavaScript在另一個網頁上顯示它。閱讀json,直到「更多」評論

我使用的是$ TUMBLR_BLOG/api/read/json feed,它提供了一個充滿博客文章信息的變量。

我想打印一切,直到"<!-- more -->"字符集的'正規體'部分,即。我不想在「常規體」中打印所有內容,直到更多部分。

有關如何做到這一點的想法?

例如, API閱讀:http://blog.intercut.yegfilm.ca/api/read/json

例如。我正在使用的基本代碼:

<script type="text/javascript" src="http://blog.intercut.yegfilm.ca/api/read/json"></script> 
<script type="text/javascript"> 
// The variable "tumblr_api_read" is now set. 
document.write(
'<h3> <a href="' + tumblr_api_read.posts[0]['url'] + '">' + tumblr_api_read.posts[0]['regular-title'] + '</a></h3>' + 
tumblr_api_read.posts[0]['regular-body']); 
</script> 

回答

2
<script type="text/javascript"> 
    // The variable "tumblr_api_read" is now set. 
    var url = tumblr_api_read.posts[0]['url']; 
    var title = tumblr_api_read.posts[0]['regular-title']; 
    var body = tumblr_api_read.posts[0]['regular-body']; 
    body = body.substring(0,body.indexOf("<!-- more -->")); 

    document.write('<h3> <a href="' + url + '">' + title + '</a></h3>' + body); 
</script> 

這麼簡單:)

0

有點像responseData.split("<!-- more -->")[0]

0

獲取<!-- more -->的索引並打印到該索引的子串。

sampleString = "Foo <!-- more --> Bar"; 

moreIndex = sampleString.indexOf("<!-- more -->"); 

if (moreIndex > 0) { 
    console.log(sampleString.substring(0, moreIndex)); 
} 

JSFiddle