normalize method
Puts the subtree that belongs to the current node into a 'normalized' form.
The normalize method joins adjacent text nodes into a single text node and removes empty text nodes.
Note: in Internet Explorer, the normalize method does not remove empty text nodes.
See the example below for details.
Syntax:
You can find the related objects in the Supported by objects section below.
Return value:
This method has no return value.
Example HTML code 1:
This example illustrates the use of the normalize method.
|
||||
<head> <script type="text/javascript"> function NormalizeAdjacentTextNodes () { var textContainer = document.getElementById ("textContainer"); var textNode1 = document.createTextNode ("First text node."); var textNode2 = document.createTextNode ("Second text node."); textContainer.appendChild (textNode1); textContainer.appendChild (textNode2); alert ("Before normalizing, the textContainer element contains " + textContainer.childNodes.length + " text nodes."); textContainer.normalize (); alert ("After normalizing, the textContainer element contains " + textContainer.childNodes.length + " text node."); alert ("The contents of the text node are:\n" + textContainer.firstChild.data); textContainer.removeChild (textContainer.firstChild); } function NormalizeEmptyTextNodes () { var textContainer = document.getElementById ("textContainer"); var emptyTextNode = document.createTextNode (""); textContainer.appendChild (emptyTextNode); textContainer.normalize (); if (textContainer.firstChild) { // Internet Explorer alert ("The normalize method does not remove empty text nodes in your browser!"); textContainer.removeChild (textContainer.firstChild); } else { alert ("The normalize method removes empty text nodes in your browser."); } } </script> </head> <body> <div id="textContainer"></div> <button onclick="NormalizeAdjacentTextNodes ();">Test the normalize method for adjacent text nodes</button> <br /><br /> <button onclick="NormalizeEmptyTextNodes ();">Test the normalize method for an empty text node</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
attribute, CommentNode, doctype, document, DocumentFragment, TextNode, XMLDocument
HTML elements:
a, abbr, acronym, address, applet, area, b, base, basefont, bdo, bgsound, big, blink, blockquote, body, br, button, caption, center, cite, code, col, colgroup, comment, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, frame, frameset, h1, h2, h3, h4, h5, h6, head, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:hidden, input:image, input:password, input:radio, input:range, input:reset, input:search, input:submit, input:text, ins, isindex, kbd, keygen, label, legend, li, link, listing, map, marquee, menu, meta, nobr, noframes, noscript, object, ol, optgroup, option, p, param, plaintext, pre, q, rt, ruby, s, samp, script, select, small, span, strike, strong, style, sub, sup, table, tbody, td, textarea, tfoot, th, thead, title, tr, tt, u, ul, var, wbr, xml, xmp
Related pages:
External links:
User Contributed Comments