简体   繁体   English

如何使用javascript innerHTML更改svg文本标记

[英]how to change svg text tag using javascript innerHTML

i was using raphaeljs , and i want to show html(not only text) in svg, 我正在使用raphaeljs,我想在svg中显示html(不仅是文本),

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 , 但我不能改变svg的内容,所以我在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 所以如何在svg上使用javscript更改文本

thanks 谢谢

SVG nodes don't have a innerHTML property (they're not HTML). SVG节点没有innerHTML属性(它们不是HTML)。

Use textContent instead: t.node.textContent='dddd' 请改用textContent: t.node.textContent='dddd'

if the code for the svg text is like this: 如果svg文本的代码是这样的:

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

if you are using JQuery try this: 如果你使用JQuery试试这个:

$("#id-of-the-text").text("new-value");

I was able to accomplish this by setting the textContent property of the <text> element. 我能够通过设置<text>元素的textContent属性来实现这一目的。 Makes it a lot easier if you know the ID of the element. 如果您知道元素的ID,则可以更轻松。

document.getElementById("id-of-text-el").textContent = "My Value";

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

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