【发布时间】:2020-01-01 11:07:39
【问题描述】:
我会将文本节点附加到 g svg 元素。
当我触发my function时,屏幕上没有打印任何内容
但是当我将TextNode 元素直接附加到正文时它起作用了
function myFunction() {
var g = document.createElementNS('http://www.w3.org/2000/svg', 'g');
var t = document.createTextNode("Hello World");
document.body.appendChild(g);
g.appendChild(t)
}
<p>Click the button to create a Text Node.</p>
<button onclick="myFunction()">Try it</button>
此代码无需使用g 即可工作
function myFunction() {
var g = document.createElementNS('http://www.w3.org/2000/svg', 'g');
var t = document.createTextNode("Hello World");
document.body.appendChild(t);
}
<p>Click the button to create a Text Node.</p>
<button onclick="myFunction()">Try it</button>
【问题讨论】:
-
创建一个 SVG
元素,而不是 HTML 文本节点。 MDN SVG 参考对所有 SVG 元素都有很好的概述。
标签: javascript html dom svg