Element: setAttributeNS() 方法
setAttributeNS 方法会添加一个具有给定命名空间和名称的新属性,或者更改一个现有属性的值。
如果您正在处理 HTML 文档,并且不需要将请求的属性指定为属于特定命名空间,请改用 setAttribute() 方法。
语法
js
setAttributeNS(namespace, name, value)
参数
返回值
无(undefined)。
示例
js
let d = document.getElementById("d1");
d.setAttributeNS(
  "http://www.mozilla.org/ns/specialspace",
  "spec:align",
  "center",
);
注意
setAttributeNS 是处理带命名空间的属性的唯一方法,它期望使用完全限定的名称,即 "namespace:local-name"。
规范
| 规范 | 
|---|
| DOM # ref-for-dom-element-setattributens① | 
浏览器兼容性
加载中…