如何在document.CreateElement之后删除属性xlmns?

时间:2016-08-03 10:00:36

标签: javascript svg createelement

我尝试在我的函数

中将文本添加到 SVG 文件中
var newText = SvgDoc.createElement("text");
newText.setAttribute( "x", 10);
newText.setAttribute("y", 10);
newText.setAttribute("font-size", 10);
newText.setAttribute("id", "newText");
var textNode = document.createTextNode("Hello world");
newText.appendChild(textNode);
SvgDoc.getElementsByTagName('svg')[0].appendChild(newText);

但它不起作用,因为SvgDoc.createElement在标记文本中添加了空属性xlmns =“”。

为了删除属性xlmns,我使用了newText.removeAttribute("xmlns");但它不起作用。

将文本添加到 Svg 文件的另一种方法是使用SvgDoc.createElementNS("http://www.w3.org/2000/svg", "text");,但我不明白为什么需要这个const字符串来创建任何元素。

0 个答案:

没有答案
相关问题