简体   繁体   English

在鼠标悬停d3js上更改SVG文本的文本

[英]Change text for an SVG text on mouseover d3js

Hi I am trying to change text of an SVG text element on mouseover. 嗨,我试图在鼠标悬停时更改SVG文本元素的文本。 My code looks like this : 我的代码如下所示:

            svg.append("text").attr("x",x).attr("y",y).text(a+'C'+(b+1)).attr("fill","blue").attr('text-anchor',"middle").on("mouseover",function(d){
                console.log("ho ho");
            });

I can see the logs being printed but I am not able to change the text for svg.append("text") Can anyone give me some hints ? 我可以看到正在打印的日志,但无法更改svg.append(“ text”)的文本。有人可以给我一些提示吗?

I have also put my code on jsfiddle http://jsfiddle.net/cma0h6zh/ 我也将代码放在jsfiddle http://jsfiddle.net/cma0h6zh/

From inside the mouseover handler, where you console.log("ho ho") , d3 sets up the this keyword such that it points to the DOM node that received the event, ie <text> in your case. 在鼠标悬停处理程序内部,您在console.log("ho ho") ,d3设置this关键字,使其指向接收事件的DOM节点,即您的情况下为<text>

So you can do whatever DOM-ish stuff you want with it, including wrapping it in a d3 selection and calling d3 methods on it: 因此,您可以使用它进行任何需要DOM的工作,包括将其包装在d3选择中并在其上调用d3方法:

d3.select(this)
  .attr('fill', 'red')
  .text('X')

Here's a modified fiddle 这是改良的小提琴

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

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