How to append characters to an SVG text element without changing its textContent value?

前端 未结 1 1022
庸人自扰
庸人自扰 2021-01-16 06:51

I am trying to append characters to an SVG text element which are separate from the text element\'s text content value. The characters need to come in front of the text ele

1条回答
  •  再見小時候
    2021-01-16 07:13

    Sounds like you want a text/tspan combination E.g.

    foobar
    

    or

    foobar
    

    This would allow you to style the foo and bar differently which seems to be what you want.

    You can write

    tspan = document.createElementNS("http://www.w3.org/2000/svg", "tspan");
    text.appendChild(tspan);
    

    to add a tspan to a text element (assuming you've got your text element in the variable called text).

    Getting the text content of the tspan just gets the tspan text content for me as one would expect. E.g.

    alert(document.getElementById("tspan").textContent);
    foobar

    0 讨论(0)
提交回复
热议问题