Parse current document as the function parameter in JavaScript

Description

The following code shows how to parse current document as the function parameter.

Example


<!--from ww w.  j  a va2  s .co  m-->
<html>
<head>
<script type="text/javascript">
function outputNodeProps(nd) {
if (nd.style) {
nd.style.backgroundColor="gray";
}
var children = nd.childNodes;
for(var i=0; i < children.length; i++) {
outputNodeProps(children[i]);
}
}
</script>
<body onload="outputNodeProps(document)">
<div id="div1">
<h1>Header</h1>
<p>P1.</p>
<p>P2</p>
<h2>Header 2</h2>
<p>P1.</p>
<p>P2</p>
</div>
</body>
</html>

Click to view the demo

The code above generates the following result.

Parse current document as the function parameter in JavaScript
Home »
  Javascript Tutorial »
    DOM »
      Check
Javascript Tutorial Check
Check if an element has a certain attribute...
Check the length of a NodeList for a div el...
Check the length of the array returned from...
Compare two nodes with isSameNode in JavaSc...
Determine the node type in JavaScript
Find out the difference between innerHTML a...
Parse current document as the function para...