简体   繁体   English

在锚标签中包装路径元素

[英]Wrapping path elements in an anchor tag

I am trying to make the <text> and <path> elements in a donut chart I have in d3 clickable, but am running into some issues. 我正在尝试使在d3中具有的甜甜圈图中的<text><path>元素可单击,但遇到了一些问题。 Here is the code I am using: 这是我正在使用的代码:

        var g = svg.append('g')
            .attr('transform', 'translate(' + width / 2 + ',' + ((height / 2)) + ')');

        var arcs = g.selectAll('path');            

        arcs = arcs.data(pie(formattedData));
        arcs.exit().remove();
        arcs.enter().append('path')
            .style('stroke', 'white')
            .attr('fill', function (d, i) { return color(i) });
        arcs.attr('d', arc);

        arcs.append("text")
            .attr("transform", function (d) { return "translate(" + labelArc.centroid(d) + ")"; })
            .attr("dy", "0em")
            .attr("style", "font-size: 1.5em;")
            .attr("fill", "white")
            .text(function (d) { return d.value; })

I can add the <a> tag around the <path> and <text> elements by editing the DOM directly in dev tools, but I can't do a .wrap() or change the code to use this either: 我可以通过直接在开发工具中编辑DOM来在<path><text>元素周围添加<a>标记,但是我无法执行.wrap()或更改代码以使用它:

.append("a").attr("href", "http://wwww.gooogle.com")

What am I missing? 我想念什么?

You should be able to do this just by appending the a element before the path or text element: 您只需将a元素附加在pathtext元素之前,就能做到这一点:

arcs.enter()
  .append('a')
    .attr('href', 'http://www.google.com')
  .append('path')
    .style('stroke', 'white')
    .attr('fill', function (d, i) { return color(i) })
    .attr('d', arc);

See https://jsfiddle.net/m4mj8u7L/1/ 参见https://jsfiddle.net/m4mj8u7L/1/

It looks like you're trying to edit SVG DOM elements with jQuery. 看来您正在尝试使用jQuery编辑SVG DOM元素。 If so, I ran into this problem myself not long ago. 如果是这样,我不久前就遇到了这个问题。 SVG elements aren't treated the same as normal HTML elements, and thus many of the jQuery functions won't affect them. SVG元素与普通HTML元素的处理方式不同,因此许多jQuery函数不会影响它们。 There's some alternate syntax that you can use to work around the limitations though. 但是,您可以使用一些替代语法来解决这些限制。 Look at this answer for some direction. 看看这个答案的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM