Specification Document: DOM Text Nodes

2019-08-04 10:29发布

I am reading a document about HTML5. A few lines down from where I linked, a sample DOM tree is displayed for the sample HTML code given. Why is there no text node directly before the <head> element? Why is there no text node between the DOCTYPE and <html> nodes? Error or feature?

2条回答
Animai°情兽
2楼-- · 2019-08-04 10:58

The text node before the <head> is probably an omission. You don't get a text node before the root element because most XML/HTML parsers can't deal with elements outside the root node, so they silently ignore them. The same happens if you add a comment or a processing instruction there.

查看更多
女痞
3楼-- · 2019-08-04 11:02

Feature. The main reason is that, given the markup

<!DOCTYPE html>
<html>
 <head>
  <title>Sample page</title>
...,

some people expect

document.documentElement.firstChild

to return the head element. However, if the text node were included, that is the node that would be returned.

(Note, also, that the new line between </body> and </html> ends up in the body element.)

查看更多
登录 后发表回答