Node: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.

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
# ref-for-dom-node-normalize①

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
normalize

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见