我正在使用D3和SVG并且有一个关于选择的问题.
我在DOM中有一个元素,如下所示:
// some other code Testing D3
现在我想使用选择获取文本"Testing D3".我该怎么做?
我尝试过以下方法:
d3.select(".tooltip .name").text d3.select(".tooltip .name").value d3.select(".tooltip .name").textContent d3.select(".tooltip .name").html
但他们都没有工作.
我也试过用
d3.select(".tooltip .name").html(this.value);
正如这里和这里所建议的那样,但这给了我一个SVGTextElement
而不是实际价值.
我也不能使用document.getElementById("name");
,如建议在这里,因为我已经实现仅通过D3此功能.
请注意,名称是单个元素.
你需要一个子选择器(并调用该.text()
函数):
d3.select("g.tooltip > text.name").text();