在可折叠树上添加节点文本的超链接

use*_*978 9 d3.js

我想在可折叠树示例中的节点文本上添加超链接.

我怎样才能做到这一点?

Mar*_*ijn 6

我是一个Javascript/svg/d3js noob,但我通过在文本上放置一个超链接的透明矩形来"解决"这个问题,这个解决方法可以作为bl.ock使用.:

nodeEnter
  .append("a")
     .attr("xlink:href", function (d) { return "http://www.example.com/flare/" + d.id; })
  .append("rect")
      .attr("class", "clickable")
      .attr("y", -6)
      .attr("x", function (d) { return d.children || d._children ? -60 : 10; })
      .attr("width", 50) //2*4.5)
      .attr("height", 12)
      .style("fill", "lightsteelblue")
      .style("fill-opacity", .3)        // set to 1e-6 to make transparent          
      ;
Run Code Online (Sandbox Code Playgroud)

我添加了一个额外的clickable样式添加.on("click", click)到圆圈而不是group(g)元素.

这有效,但缺点是可点击的rect的大小不会与标签的文本一致.

我真的很期待一个更好的解决方案,所以+1为你的问题!


emp*_*emp 5

如果删除全局节点上的单击处理程序并附加2个不同的单击处理程序:

  • 一个圆圈
  • 一个用于文本

单击文本时,您可以有不同的行为.如果您将该元素设置为一个样式,它可以看起来完全像一个超链接.

看看我的小提琴:http: //jsfiddle.net/empie/EX83X/

单击处理程序

var nodeEnter = node.enter().append("g")
          .attr("class", "node")
          .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; });

      nodeEnter.append("circle")
          .attr("r", 1e-6)
          .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; }).on("click", click);;

      nodeEnter.append("text")
          .attr("x", function(d) { return d.children || d._children ? -10 : 10; })
          .attr("dy", ".35em")
          .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
          .text(function(d) { return d.name; })
          .style("fill-opacity", 1e-6)
      .attr("class", "hyper").on("click", clack);

    function clack(d) {
        alert(d.name);
    }
Run Code Online (Sandbox Code Playgroud)

和CSS:

.hyper {
    color: blue;
    text-decoration: underline;
}

.hyper:hover {
    color: yellow;
    text-decoration: none;
}
Run Code Online (Sandbox Code Playgroud)