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的名稱空間字首。