问题描述
我无法解决这个问题。我使用 setAttributeNS
在我的 SVG 元素上设置 xml:space
属性,因为该属性是在 here 中定义的,但浏览器抱怨它应该直接通过 setAttribute
设置。显然这就是我试图理解为什么的解决方案?
如果命名空间不正确,我如何确定某些属性是否使用命名空间?其背后的原因是我尝试序列化节点。有一种获取 namespace of element 的方法,但我找不到任何可以从属性中获取命名空间的方法,一旦我在其他地方呈现序列化节点,这对我来说至关重要。
这是一个例子:
<svg viewBox="0 0 140 50" xmlns="http://www.w3.org/2000/svg">
<text id="spacing" y="20" xml:space="default">Default spacing</text>
</svg>
// works
document.getElementById("spacing").setAttribute("xml:space","preserve");
// not working
// Failed to execute 'setAttributeNS' on 'Element': 'https://www.w3.org/XML/1998/namespace' is // an invalid namespace for attributes.
document.getElementById("spacing").setAttributeNS(
"https://www.w3.org/XML/1998/namespace","xml:space","preserve"
);
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)