XML DOM hasAttributeNS() 方法
XML基础 2023-08-13 15:00:26小码哥的IT人生shichen
XML DOM hasAttributeNS() 方法
定义和用法
如果属性由指定的命名空间和名称组成,则 hasAttributeNS() 方法返回 true,否则返回 false。
语法:
hasAttributeNS(ns,name)
参数 | 描述 |
---|---|
ns | 必需。规定要检索的属性的命名空间。 |
name | 必需。规定要检索的属性的名称。 |
说明
该方法与 hasAttribute() 方法类似,只是要检查的属性由命名空间和名称指定。只有使用命名空间的 XML 文档才使用方法。
实例
在所有的例子中,我们将使用 XML 文件 books_ns.xml,以及 JavaScript 函数 loadXMLDoc()。
下面的代码片段检查 "books_ns.xml" 中的第一个 <title> 元素是否拥有带有指定的命名空间和名称的属性:
xmlDoc=loadXMLDoc("books_ns.xml");
x=xmlDoc.getElementsByTagName("title")[0];
ns="http://www.phpcodeweb.com/children/";
document.write(x.hasAttributeNS(ns,"lang")
);
以上代码的输出:
true
TIY
完整实例【hasAttributeNS - 检查元素是否拥有带有指定的命名空间和名称的属性】:
<html>
<head>
<script type="text/javascript" src="/demo/example/xdom/loadxmldoc.js">
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("/demo/example/xdom/books_ns.xml");
x=xmlDoc.getElementsByTagName("title")[0];
ns="http://www.phpcodeweb.com/children/";
document.write(x.hasAttributeNS(ns,"lang"));
</script>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html