1 に答える
<html>
, <body>
, and <head>
are expected to be the root of the document, so they will be ignored when they are not at the root.
When you add <code>
at the root, the browser inserts <html><body>
before it automatically because it knows it is supposed to be there, and then it will ignore the others because they are not at the root.
That means that what actually ends up in the parsed DOM tree is this:
<html>
<head>
</head>
<body>
<code>
<div>
Foo!
</div>
</code>
</body>
</html>
Thus, when your code runs, it gets everything inside of <code>
.
If you want to display all if this as text, you should be encoding the HTML as text, using <
and such, and then use .text()
instead.
Like this: http://jsfiddle.net/eqnK4/1/