--- title: Element.hasAttributeNS() slug: Web/API/Element/hasAttributeNS tags: - API - 元素 - 属性 - 方法 translation_of: Web/API/Element/hasAttributeNS ---
{{ APIRef("DOM") }}
hasAttributeNS 返回一个布尔值,指示该元素是否包含有指定的属性(attribute)。
result =element.hasAttributeNS(namespace,localName)
result 为返回的布尔值:true 或 false。namespace 是一个字符串,表示属性的命名空间。localName 是一个字符串,表示属性的名称。// 在为属性设置值之前检测该属性是否存在
var d = document.getElementById("div1");
if (d.hasAttributeNS(
"http://www.mozilla.org/ns/specialspace/",
"special-align")) {
d.setAttribute("align", "center");
}
该方法与hasAttribute类似,只是要检查的属性由命名空间和名称指定。只有使用命名空间的 XML 文档才使用方法
{{ DOMAttributeMethods() }}