Specification Document: DOM Text Nodes

╄→гoц情女王★ 提交于 2020-01-15 12:09:16

问题


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?


回答1:


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.)




回答2:


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.



来源:https://stackoverflow.com/questions/3227416/specification-document-dom-text-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!