The normalize()
method removes empty Text nodes, and joins adjacent Text nodes.
normalize() |
Yes | Yes | Yes | Yes | Yes |
node.normalize()
None.
No return value.
The following code shows how to normalize an element.
<!DOCTYPE html>
<html>
<body>
<p id="demo">test<p>test</p><p>test</p><p></p></p>
<!-- w w w . jav a 2 s. co m-->
<button onclick="addTextNode()">Add a Text Node</button>
<button onclick="normPara()">Normalize the paragraph</button>
<script>
function addTextNode()
{
var y=document.createTextNode(" test ");
var x=document.getElementById("demo");
x.appendChild(y);
var z=document.getElementById("cc");
z.innerHTML=x.childNodes.length;
}
function normPara()
{
var x=document.getElementById("demo");
x.normalize();
var z=document.getElementById("cc");
z.innerHTML=x.childNodes.length;
}
</script>
<span id="cc">X</span>
</body>
</html>
The code above is rendered as follows: