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(),其反向操作。