我正在使用 d3 绘制图表(我正在使用 thix 示例: https://gist.github.com/keiranlovett/8766741 )。我需要添加一个链接。我正在添加文本并试图将其设为链接,但它并未显示为链接(我使用的是 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);
});
我还添加了来自 http://jsfiddle.net/christopheviau/B9zcF/ 的一些代码行使文本成为链接,但它不起作用。
如何使我的文本成为链接?
最佳答案
这也是一个很好的解决方案:
d3.select(this)
.text(d)
.on("click", function() { window.open("https://www.random.com/" + d ); });
关于d3.js - 使文本成为 svg 中的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34920956/