XML DOM setAttributeNS()メソッド
❮要素オブジェクト
例
次のコードフラグメントは、「books_ns.xml」をxmlDocにロードし、最初の<book>要素に「edition」属性を追加します。
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books_ns.xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var x =
xmlDoc.getElementsByTagName("book")[0];
var ns =
"https://www.w3schools.com/edition/";
x.setAttributeNS(ns,
"edition", "first");
document.getElementById("demo").innerHTML
=
x.getAttributeNS(ns,"edition");
}
出力:
first
定義と使用法
setAttributeNS()メソッドは、(名前空間を使用して)新しい属性を追加します。
その名前または名前空間の属性が要素にすでに存在する場合、その値はプレフィックスおよび値パラメーターの値に変更されます
構文
elementNode.setAttributeNS(ns,name,value)
Parameter | Description |
---|---|
ns | Required. Specifies the namespace URI of the attribute to set |
name | Required. Specifies the name of the attribute to set |
value | Required. Specifies the value of the attribute to set |
例
次のコードフラグメントは、「books_ns.xml」をxmlDocにロードし、最初の<title>要素の「lang」値を変更します。
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
myFunction(xhttp);
}
};
xhttp.open("GET", "books_ns.xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var x =
xmlDoc.getElementsByTagName("title")[0];
var ns =
"https://www.w3schools.com/edition/";
x.setAttributeNS(ns,
"c:lang", "italian");
document.getElementById("demo").innerHTML
=
x.getAttributeNS(ns, "lang");
}
出力:
italian
❮要素オブジェクト