javascript - d3.js 中的链接强制定向图

标签 javascript svg append hyperlink d3.js

this example , 如何让每个节点的文本成为可点击的链接?

我尝试了与此代码类似的操作,但这些值不可点击:

var links = text.append("a").attr("xlink:href", "http://www.google.com/");

// A copy of the text with a thick white stroke for legibility.
links.append("svg:text")
    .attr("x", 8)
    .attr("y", ".31em")
    .attr("class", "shadow")
    .text(function(d) { return d.name; });

 links.append("svg:text")
    .attr("x", 8)
    .attr("y", ".31em")
    .text(function(d) { return d.name; });

编辑/解决方案:结果是 css 有这个属性:pointer-events: none; 我不得不删除它,然后按照 Elijah 的建议使用。

最佳答案

不要使用链接,将其删除并直接 append 到您的文本中 <g>它应该有效。

text.append("svg:text")
        .attr("x", 8)
        .attr("y", ".31em")
        .attr("class", "shadow")
        .text(function(d) { return d.name; })
        .on("click", function() {yourFunction(yourVar)})
        .on("mouseover", function() {yourFunction2(yourVar)})
        .on("mouseout", function() {yourFunction3(yourVar)})
;

另外,如果你想传递绑定(bind)数据,你可以这样做:

.on("click", function(d) {yourFunction(d.yourVar)}

而如果你想传递实际的 d 对象,你可以这样做:

.on("click", yourFunction}

在这种情况下,yourFunction(d,i) 可以从您的绑定(bind)数据中引用 d.whatever。

关于javascript - d3.js 中的链接强制定向图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12589940/

相关文章:

javascript - 如何使用 cypress.io 检查嵌套的阴影元素

python - 如果为空,则 append 到 DataFrame 时出现问题

append - Libreoffice/Openoffice Calc - 将字符串 append 到单元格

javascript - 在 Vue.js 中进行 AJAX 调用后重新加载轮播

javascript - 有没有什么好的方法可以在rest api中传递错误对象

javascript - 从canvas元素获取SVG并保存

javascript - 将参数添加到 d3 拖动行为后,元素不再可拖动

python - 为什么 append() 在 Python 中总是返回 None?

javascript - TypeScript:导入的模块未编译

javascript - Div 占用了额外的空间