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① |
瀏覽器相容性
載入中…