2014-10-11 51 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