Node:normalize() 方法
normalize() 方法是 Node 接口的一个方法,它会将指定节点及其所有子树规范化。在规范化后的子树中,子树中的文本节点不会为空,并且不会有相邻的文本节点。
语法
js
normalize()
参数
无。
返回值
无。
示例
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 = "Before normalization:\n";
while (node) {
result += ` ${node.nodeName}: ${node.nodeValue}\n`;
node = node.nextSibling;
}
wrapper.normalize();
node = wrapper.firstChild;
result += "\n\nAfter normalization:\n";
while (node) {
result += ` ${node.nodeName}: ${node.nodeValue}\n`;
node = node.nextSibling;
}
const output = document.getElementById("result");
output.innerText = result;
规范
| 规范 |
|---|
| DOM # ref-for-dom-node-normalize① |
浏览器兼容性
加载中…
另见
Text.splitText(),其反向操作。