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 = "正規化前:<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

関連情報