Node:normalize() 方法

Node 接口的 normalize() 方法将指定的节点及其所有子树转化为规范化形式。在规范化子树中,子树上的文本节点都不为空,且没有相邻的文本节点。

语法

js
normalize()

参数

无。

返回值

无。

示例

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

wrapper.appendChild(document.createTextNode("第 1 部分"));
wrapper.appendChild(document.createTextNode("第 2 部分"));

let node = wrapper.firstChild;
let result = "规范化之前:<br/>";
while (node) {
  result += ` ${node.nodeName}${node.nodeValue}<br/>`;
  node = node.nextSibling;
}

wrapper.normalize();

node = wrapper.firstChild;
result += "<br/><br/>规范化之后:<br/>";
while (node) {
  result += ` ${node.nodeName}${node.nodeValue}<br/>`;
  node = node.nextSibling;
}

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

规范

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

浏览器兼容性

BCD tables only load in the browser

参见