CharacterData: thuộc tính nextElementSibling

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 nextElementSibling của giao diện CharacterData trả về nút Element đầu tiên đứng sau nút được chỉ định trong danh sách phần tử con của nút cha của nó, hoặc null nếu phần tử được chỉ định là phần tử cuối cùng trong danh sách.

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
TEXT
<div id="div-01">Here is div-01</div>
TEXT2
<div id="div-02">Here is div-02</div>
<pre>Here is the result area</pre>
js
// Ban đầu, đặt node thành nút Text chứa `TEXT`
let node = document.getElementById("div-01").previousSibling;

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

while (node) {
  result += `${node.nodeName}\n`;
  node = node.nextElementSibling; // Nút đầu tiên là CharacterData, các nút còn lại là đối tượng Element
}

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

Thông số kỹ thuật

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

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

Xem thêm