XML DOM removeAttributeNS() 方法

定義和用法

removeAttributeNS() 方法刪除由命名空間和名稱指定的屬性。

語法

elementNode.removeAttributeNS(ns,name)
參數 描述
ns 必需。規定要刪除的屬性的命名空間。
name 必需。規定要刪除的屬性的名稱。

實例

下面的代碼將 "books_ns.xml" 加載到 xmlDoc 中,并從第一個 <title> 元素中刪除 "lang" 屬性:

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("title")[0];
    var ns = "https://www.codew3c.com/meishi/";
    document.getElementById("demo").innerHTML =
    "Attribute Found: " + x.hasAttributeNS(ns, "lang");
    x.removeAttributeNS(ns, "lang");
    document.getElementById("demo").innerHTML +=
    "<br>Attribute Found: " + x.hasAttributeNS(ns, "lang");
}

親自試一試