【发布时间】:2016-09-09 07:29:55
【问题描述】:
我试图在纯 JS 中操作 SVG,发现如果我不使用 createElementNS 和 setAttributeNS 之类的方法,它的行为将无法达到预期。
<svg id="mydsvg" width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
上述标记完美运行。但是如果您尝试通过以下代码添加另一个圈子,您将看不到它。
var circle = createElement('circle');
circle.setAttribute('cx', 50);
....
document.getElementById('mysvg').appendChild(circle);
但如果你使用createElementNS 和setAttributeNS,它会按预期工作。
最糟糕的是,createElement 和 createElementNS 都会创建相同的 DOM 文本。
【问题讨论】:
标签: javascript svg namespaces