Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript xml:无法通过element.setAttributeNS()方法设置空间属性_Javascript_Javascript Namespaces - Fatal编程技术网

Javascript xml:无法通过element.setAttributeNS()方法设置空间属性

Javascript xml:无法通过element.setAttributeNS()方法设置空间属性,javascript,javascript-namespaces,Javascript,Javascript Namespaces,我无法理解这个问题。我使用setAttributeNS在SVG元素上设置属性,因为属性是在中定义的,但浏览器抱怨应该通过setAttribute直接设置属性。显然这就是解决办法我只是想弄明白为什么 如果名称空间不正确,如何查找某些属性是否使用名称空间?背后的原因是我试图序列化节点。有一种获取名称空间的方法,但我找不到任何从属性中获取名称空间的方法,一旦我在其他地方呈现序列化节点,这对我来说至关重要 以下是一个例子: <svg viewBox="0 0 140 50" x

我无法理解这个问题。我使用
setAttributeNS
在SVG元素上设置属性,因为属性是在中定义的,但浏览器抱怨应该通过
setAttribute
直接设置属性。显然这就是解决办法我只是想弄明白为什么

如果名称空间不正确,如何查找某些属性是否使用名称空间?背后的原因是我试图序列化节点。有一种获取名称空间的方法,但我找不到任何从属性中获取名称空间的方法,一旦我在其他地方呈现序列化节点,这对我来说至关重要

以下是一个例子:

<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"
  );