Attr: prefix 属性
Attr 接口的只读 prefix 属性返回属性的命名空间前缀,如果未指定前缀,则返回 null。
无论在属性创建时使用何种大小写,前缀始终是小写。
注意: 只有 XML 支持命名空间。HTML 不支持。这意味着 HTML 元素的属性前缀将始终是 null。
此外,仅支持 xml(用于 xml:lang 属性)、xlink(用于 xlink:href、xlink:show、xlink:target 和 xlink:title 属性)以及 xpath 命名空间,并且仅在 SVG 和 MathML 元素上支持。
值
一个包含属性所属命名空间前缀的字符串。如果没有,则返回 null。
示例
HTML
html
<svg xml:lang="en-US" class="struct" height="1" width="1">Click me</svg>
<label xml:lang="en-US" class="struct"></label>
<p>
<button>Show value for <svg></button>
<button>Show value for <label></button>
</p>
<p>
Prefix of the attribute <code>xml:lang</code>:
<output id="result">None.</output>
</p>
JavaScript
js
const elements = document.querySelectorAll(".struct");
const buttons = document.querySelectorAll("button");
const outputEl = document.querySelector("#result");
let i = 0;
for (const button of buttons) {
const element = elements[i];
button.addEventListener("click", () => {
const attribute = element.attributes[0];
outputEl.value = attribute.prefix;
});
i++;
}
规范
| 规范 |
|---|
| DOM # dom-attr-prefix |
浏览器兼容性
加载中…
另见
Attr.name属性返回属性的限定名称,Attr.localName属性返回其本地名称。Element.prefix()属性,返回Element的命名空间前缀。