D3.js 使文本成为svg中的链接

D3.js 使文本成为svg中的链接,d3.js,svg,hyperlink,D3.js,Svg,Hyperlink,我使用d3绘制图表,我使用thix示例:。我需要添加一个链接。我正在添加文本,并试图将其作为链接,但它没有显示为链接,我正在使用IE 11: svg.append("g") //without doing this, impossible to put grid lines behind text .attr({ xmlns: "http://www.w3.org/2000/svg", xlink: "http://www.w3.o

我使用d3绘制图表,我使用thix示例:。我需要添加一个链接。我正在添加文本,并试图将其作为链接,但它没有显示为链接,我正在使用IE 11:

svg.append("g") //without doing this, impossible to put grid lines behind text  

     .attr({
            xmlns: "http://www.w3.org/2000/svg",
            xlink: "http://www.w3.org/1999/xlink",
            width: 100,
            height: 300
        })
     .attr({ "xlink:href": "#" })
     .on("mouseover", function (d, i) {
            //alert('aaa');
            d3.select(this)
                .attr({ "xlink:href": "http://example.com/" + "eeeeeeeeeeeeeeeeeee" });
        })
     .selectAll("text")

     .data(tasks)
     .enter()
     .append("text")

     .text(function (d) {
         if (ammendmentsLinks.indexOf(d.contract) < 0) {
             ammendmentsLinks = ammendmentsLinks + d.contract + ";";
             return d.contract;
             //return "";
         }
         else {
             return "";
         }
     })
     .attr("x", 20)
     .attr("y", function (d, i) {
         return i * 20;
     })
     .attr("font-size", 11)
     .attr("text-anchor", "start")
     .attr("text-height", 14)
     .on("click", function (d) {
         return click(d.contract);
     });
我还从中添加了一些代码行,使文本成为链接,但它不起作用


如何使我的文本成为链接?

尝试更改以下行:

d3.select(this)
            .attr({ "xlink:href": "http://example.com/" + "eeeeeeeeeeeeeeeeeee" });


并确保标记具有xmlns:xlink=http://www.w3.org/1999/xlink 指定。

这也是一个很好的解决方案:

  d3.select(this)
              .text(d)
              .on("click", function() { window.open("https://www.random.com/" + d ); }); 

您需要将元素包装在一个元素中。您能接受我的代码并更新它吗?我是svg新手,我没有时间查看文档,我需要完成一项任务来制作这个图表,我很难让它工作。Thanks@RobertLongson事实上,这是有效的,我不知道JSFIDLE中的元素来自html。Thanksd3版本5适用于此。换行不是另一个答案的函数
  d3.select(this)
              .text(d)
              .on("click", function() { window.open("https://www.random.com/" + d ); });