xml:space 属性不能通过 element.setAttributeNS() 方法设置

问题描述

我无法解决这个问题。我使用 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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...