简体   繁体   中英

Select content of text element with tspan specifier in SVG with D3js

I add a text to an SVG by D3js

var svg = d3.select("body").append("svg");
var t = svg.append("text");
t.append("tspan").attr("class", "span1").text("This is span 1");
t.append("tspan").attr("class", "span2").text("and this is 2");

The SVG element is added and is

<text> <span class="span1">This is span 1</span> <span class="span2">and this is 2</span> </text>

How can I retrieve the whole content of the text element with the span specifiers?

The text is available as

var text = t[0][0].textContent;

but it does contain "This is span1and this is 2" without the span specifiers!

在这种情况下,您可以使用innerHTML

var text = t[0][0].innerHTML;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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