【发布时间】:2017-08-03 05:00:32
【问题描述】:
我正在尝试使用 appendChild() 方法在 SVG 中插入文本。我需要将这种方法用于我正在编写的实际代码。
在附加的代码中,您会发现我正在尝试编写字符串“你好吗?”在 SVG 中使用 appendChild() 方法。但是它不显示。我使用 Chrome 开发者工具检查了执行情况,myTextElement 和 myText 实际上是在代码中执行的。不过,我不明白为什么它没有被显示。你能帮忙吗?
var mysvg = document.getElementById("mysvg")
var myTextElement = document.createElement("text");
var myText = document.createTextNode("How are you?")
myTextElement.appendChild(myText);
mysvg.appendChild(myTextElement);
circle {
fill-opacity: 0.5;
stroke-width: 4;
fill: #3080d0;
stroke: #3080d0;
opacity: 0.6;
}
<svg id="mysvg" version="1.1" xmlns="http://www.w3.org/2000/svg" width="400" height="300">
<circle id="my-circle" cx="100" cy="100" r="50" />
<text x="0" y="10" font-family="Verdana" font-size="55" fill="blue"> Hello </text>
</svg>
【问题讨论】:
-
见this answer,你需要一个有效的命名空间。
-
谢谢,瓦迪姆和亚当。在过去的 36 小时里,我都快崩溃了。
标签: javascript html css svg