Attr: thuộc tính ownerElement
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thuộc tính chỉ đọc ownerElement của giao diện Attr trả về Element mà thuộc tính thuộc về.
Giá trị
Element mà thuộc tính thuộc về, hoặc null nếu thuộc tính không được liên kết với một phần tử.
Ví dụ
Ví dụ sau hiển thị tên đủ điều kiện của phần tử của hai phần tử đầu tiên, khi chúng ta nhấn vào nút tương ứng.
HTML
html
<svg xml:lang="vi" class="struct" height="1" width="1">Click me</svg>
<label xml:lang="vi" class="struct"></label>
<p>
<button>Show value for <svg></button>
<button>Show value for <label></button>
</p>
<p>
Tên đủ điều kiện của phần tử sở hữu thuộc tính <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.ownerElement.tagName.toLowerCase();
});
i++;
}
Thông số kỹ thuật
| Specification |
|---|
| DOM> # dom-attr-ownerelement> |