2013-10-17 52 views
0

URL链接,我在JSON数据是这样的:d3.js - 如何添加在折叠缩进树

{ 
"name": "OSS Applications", 
"children": [ 
    { 
    "name": "ELITE 3E", 
    "children": [ 
     {"name": "Elite 3E Mobile Gateway"}, 
     {"name": "Elite 3E Mobile Website"}, 
     {"name": "Elite 3E Mobile Application"} 

    ] 
    }, 
    { 
    "name": "WESTLAW DOC & FORM BUILDER", 
    "children": [ 
     {"name": "Cobalt Website WFB"}, 
     {"name": "Cobalt Static Content WFB"}, 
     {"name": "Cobalt Search WFA"}, 
     {"name": "Cobalt Forms Assembly WFB"}, 
     {"name": "Cobalt Foldering WFB"} 
    ] 
    }, 
    { 
    "name": "FINDLAW.COM", 
    "children": [ 
     {"name": "Findlaw-Infirmation"}, 
     {"name": "Findlaw-hippocms"}, 
     {"name": "Findlaw-Lawyers Directory"}, 
     {"name": "Findlaw-ProfileUpdate"}, 
     {"name": "Findlaw-Pview"}, 
    {"name": "Findlaw-RCMS"}, 
    {"name": "Findlaw-public-channel"} 
    ] 
    } 
] 
} 

我想一个URL链接添加到应用程序“ELITE 3E”,“ WESTLAW DOC & FORM BUILDER“和”FINDLAW.com“。我怎么做?我正试图复制可折叠的缩进树。 mbostock的块#1093025

+0

您可以使用[SVG链接](http://www.w3.org/wiki/SVG_Links)。 –

+0

谢谢拉尔斯....我从这个http://stackoverflow.com/questions/13104681/hyperlinks-in-d3-js-objects得到了一个类似的例子。但是,如何在单独的选项卡而不是同一个窗口中打开href –

+0

指定'target =“_ blank”'。 –

回答

3

您可以使用SVG link来实现此目的。您需要做的就是在要用作链接的元素周围添加一个a元素,例如,

svg.append("a") 
    .attr("xlink:href", function(d) { return d.url; }) 
    .append("circle") 
    //etc 

,使链接在新窗口中打开,target属性设置为_blank

svg.append("a") 
    .attr("xlink:href", function(d) { return d.url; }) 
    .attr("target", "_blank") 
0

我用来添加下面的链接,你可以添加ATTR的onclick。

<text x="7" y="20" style="font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif;font-size:12px;color:white;fill:white;" zIndex="1"> 
    <tspan onclick="location.href="http://target.url.com"" style="cursor: pointer;" x="7">URL TEXT</tspan> 
</text> 

另一种方式,你可以绑定click事件来实现它。

svg.selectAll('.add-url-node') 
    .on('click',function(d){ 
     location.href = 'target.url.com'; 
    });