2014-10-11 84 views
0
function (data) { 
    var json = data; 

    if (json["type"] == "story") { 
     $(".title").append("<p><a href=" + json["url"] + ">" + json["title"] + "</a> " + json["id"] + "</p>"); 
    } 
} 

當我做到這一點我想什麼是一樣的東西jQuery的.append和鏈接

<p><a href="http://www.vanityfair.com/business/2014/11/satya-nadella-bill-gates-steve-ballmer-microsoft.print">The Microsoft Empire Reboots</a> 8441095</p> 

,但在某些情況下,我得到

<p><a href="https://firstlook.org/theintercept/2014/10/10/core-secrets"></a>Core Secrets: NSA Saboteurs in China and Germany 8441055</p> 

的以.json爲:https://hacker-news.firebaseio.com/v0/item/8441055.json?print=pretty

{ 
    "by" : "patrickgokey", 
    "id" : 8441055, 
    "kids" : [ 8441399, 8441097, 8441985, 8441301, 8441852, 8441578, 8441287, 8441389, 8441586, 8441583, 8441584, 8441449, 8441380 ], 
    "score" : 161, 
    "text" : "", 
    "time" : 1412995787, 
    "title" : "Core Secrets: NSA Saboteurs in China and Germany", 
    "type" : "story", 
    "url" : "https://firstlook.org/theintercept/2014/10/10/core-secrets/" 
} 

這是由錯誤引起的.append函數?

+3

它取決於你的JSON數據 – 2014-10-11 12:43:18

+0

你需要證明導致問題的數據,這樣我們就可以複製它。 – JJJ 2014-10-11 12:45:20

+0

我添加了.json – 2014-10-11 12:57:30

回答

1

您需要在href屬性報價:

if (json["type"] == "story") { 
    $(".title").append("<p><a href='" + json["url"] + "'>" + json["title"] + "</a> " + json["id"] + "</p>"); 
} 

http://jsfiddle.net/pjammox5/1/

要知道爲什麼,看看差別網址:

http://www.vanityfair.com/business/2014/11/satya-nadella-bill-gates-steve-ballmer-microsoft.print https://firstlook.org/theintercept/2014/10/10/core-secrets/

注二等,-secrets/結束,與<a href=>結合,自閉:

<a href=https://firstlook.org/theintercept/2014/10/10/core-secrets/> <--Closes the tag