how to change svg text tag using javascript innerH

2019-04-18 03:05发布

i was using raphaeljs , and i want to show html(not only text) in svg,

so i use this code :

var r = Raphael("holder", 200, 300);
var t = r.text(10, 10, "ssdwqdwq");
t.node.innerHTML='dddd'

but i cant change the svg's content , so i console it in firebug ,

console.log(t.node)

it show this :

<text x="10" y="13.5" text-anchor="middle" style="font: 10px "Arial";" font="10px "Arial"" stroke="none" fill="#000000">

so how to change the text using javscript on svg

thanks

3条回答
Bombasti
2楼-- · 2019-04-18 03:17

SVG nodes don't have a innerHTML property (they're not HTML).

Use textContent instead: t.node.textContent='ffffdd'

查看更多
等我变得足够好
3楼-- · 2019-04-18 03:23

I was able to accomplish this by setting the textContent property of the <text> element. Makes it a lot easier if you know the ID of the element.

document.getElementById("id-of-text-el").textContent = "My Value";
查看更多
【Aperson】
4楼-- · 2019-04-18 03:25

if the code for the svg text is like this:

<text id="id-of-the-text"> old value</text>

if you are using JQuery try this:

$("#id-of-the-text").text("new-value");
查看更多
登录 后发表回答