Element: setAttributeNS() メソッド
Baseline
広く利用可能
この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2015年7月以降、すべてのブラウザーで利用可能です。
setAttributeNS は、指定された名前空間と名前で、新しい属性を追加したり、属性の値を変更したりします。
構文
js
setAttributeNS(namespace, name, value)
引数
namespaceは属性の名前空間を指定する文字列です。nameは修飾名で属性を識別する文字列です。すなわち、名前空間接頭辞に続いてコロン、続いてローカル名です。valueは新しい属性の希望する文字列値です。
返値
なし (undefined)。
例
js
let d = document.getElementById("d1");
d.setAttributeNS(
"http://www.mozilla.org/ns/specialspace",
"spec:align",
"center",
);
メモ
setAttributeNS は名前空間属性のための唯一のメソッドで、完全修飾名、つまり "namespace:localname" を期待します。
仕様書
| 仕様書 |
|---|
| DOM> # ref-for-dom-element-setattributens①> |