Taking the below as an example:
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<script>
document.write("<p>My First JavaScript</p>");
</script>
<h1>My Second Heading</h1>
</body>
</html>
What are the children of Element body? Will it contain a script which itself contains a p, or will it contain a p directly?
Neither.
It will contain the
scriptelement, and right after that comes thepelement that script writes to the document.If you examine the nodes that end up in the body element, you get something like this:
The exact naming of DOM element vary, and the text nodes for the whitespace in the document varies between browsers depending on how the HTML is parsed. IE7 and IE8 for example reports only the four objects (h1, script, p, h1), the whitespace is not included as elements:
Demo: http://jsfiddle.net/J69UA/1/