Node: phương thức normalize()

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.

Phương thức normalize() của giao diện Node đưa nút đã chỉ định và toàn bộ cây con của nó vào dạng chuẩn hóa. Trong một cây con đã chuẩn hóa, không có nút văn bản nào là rỗng và không có các nút văn bản liền kề nhau.

Cú pháp

js
normalize()

Tham số

Không có.

Giá trị trả về

Không có.

Ví dụ

html
<output id="result"></output>
js
const wrapper = document.createElement("div");

wrapper.appendChild(document.createTextNode("Part 1 "));
wrapper.appendChild(document.createTextNode("Part 2 "));

let node = wrapper.firstChild;
let result = "Trước khi chuẩn hóa:\n";
while (node) {
  result += ` ${node.nodeName}: ${node.nodeValue}\n`;
  node = node.nextSibling;
}

wrapper.normalize();

node = wrapper.firstChild;
result += "\n\nSau khi chuẩn hóa:\n";
while (node) {
  result += ` ${node.nodeName}: ${node.nodeValue}\n`;
  node = node.nextSibling;
}

const output = document.getElementById("result");
output.innerText = result;

Thông số kỹ thuật

Specification
DOM
# ref-for-dom-node-normalize①

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

Xem thêm