DOM Element
对象removeAttributeNS()
方法通过指定的本地名称和名称空间URI删除属性。
语法
以下是removeAttributeNS()
方法的使用语法。
elementObj.removeAttributeNS(namespace,attrName)
参数
namespace
- 它是一个指定属性名称空间的字符串。attrName
- 它是一个字符串,用于要从当前节点中删除的属性名称。
返回值
- 它返回指定属性的
Attr
节点。
示例
- 文件node_ns.xml 的内容如下 -
<?xml version = "1.0"?>
<Company>
<Employee xmlns:e = "http://www.yiibai.com/technical/" category = "technical">
<e:FirstName e:lang = "en">Tianya</e:FirstName>
<e:LastName>Su</e:LastName>
<e:ContactNo>0898-1234567890</e:ContactNo>
<e:Email>tanmaypatil@xyz.com</e:Email>
</Employee>
<Employee xmlns:n = "http://www.yiibai.com/non-technical/" category = "non-technical">
<n:FirstName n:lang = "en">Taniya</n:FirstName>
<n:LastName>Lee</n:LastName>
<n:ContactNo>0898-1234567890</n:ContactNo>
<n:Email>tanyia@yiibai.com</n:Email>
</Employee>
</Company>
以下示例演示了removeAttributeNS()
方法的用法 -
<!DOCTYPE html>
<html>
<meta charset="utf-8"/>
<head>
<script>
function loadXMLDoc(filename) {
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else // code for IE5 and IE6
{
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
</script>
</head>
<body>
<script>
xmlDoc = loadXMLDoc("/node_ns.xml");
x = xmlDoc.getElementsByTagName("FirstName")[0];
ns = "http://www.yiibai.com/technical/";
document.write("Before removing the attributeNS: ");
document.write(x.getAttributeNS(ns,"lang"));
x.removeAttributeNS(ns,"lang");
document.write("<br>After removing the attributeNS: ");
document.write(x.getAttributeNS(ns,"lang"));
</script>
</body>
</html>
执行上面示例代码,得到以下结果 -
Before removing the attributeNS: en
After removing the attributeNS: null