CharacterData: thuộc tính previousElementSibling

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.

Thuộc tính chỉ đọc previousElementSibling của giao diện CharacterData trả về nút Element đầu tiên đứng trước nút hiện tại trong danh sách phần tử con của nút cha của nó, hoặc null nếu không có.

Giá trị

Một đối tượng Element, hoặc null nếu không tìm thấy nút anh em nào.

Ví dụ

html
<div id="div-01">Here is div-01</div>
TEXT
<div id="div-02">Here is div-02</div>
SOME TEXT
<div id="div-03">Here is div-03</div>
<pre>Result</pre>
js
// Ban đầu đặt node thành nút Text chứa `SOME TEXT`
let node = document.getElementById("div-02").nextSibling;

let result = "Previous element siblings of SOME TEXT:\n";

while (node) {
  result += `${node.nodeName}\n`;
  node = node.previousElementSibling;
}

document.querySelector("pre").textContent = result;

Thông số kỹ thuật

Specification
DOM
# ref-for-dom-nondocumenttypechildnode-previouselementsibling②

Tương thích trình duyệt

Xem thêm