2012年10月9日 星期二

為什麼 table 被載入到 DOM 時, 會多加 tbody tag


今天看別人上課的ppt, 裡面問到一個問題:
為什麼 table 被載入到 DOM 時, 會多加 tbody tag?

--------------
Nickolay 說:

To summarize the excellent explanations given in the answers and comments by bobince, Kieron, Alohci and others:
  1. Firebug just displays the DOM of the parsed page. Due to complicated HTML parsing rules, the DOM will "differ" (in some sense) from the source HTML.
  2. In this case the TBODY element in the DOM is added by the HTML parser. Note that this weird parsing is limited to text/html documents and in XHTML the DOM corresponds closely to the source XML.
    • This behavior was specified in HTML 4. The content model (allowed children) for table is(CAPTION?, (COL*|COLGROUP*), THEAD?, TFOOT?, TBODY+) -- trs are only allowed intbody! The spec says that tbody's start tag is optional, which is supposed to mean that if the HTML parser encounters tr directly inside a table it inserts the tbody start tag omitted by the author.
    • To make matters more clear HTML 5 defines very detailed parsing rules, in particular for this case: "When the insertion mode is "in table", tokens must be handled as follows: [...] A start tag whose tag name is one of: "td", "th", "tr" -> Act as if a start tag token with the tag name "tbody" had been seen, then reprocess the current token."


--------------
原來 tbody 是一個重要的 tag, 下面這篇文章明確指出, 如果用 DOM 方法建立 table 時, IE 不會自己加一個 tbody, 此時當我們試圖添加 tr 在 table 之下時, IE 也不會成功。

A bug about table in M$IE
http://blog.roodo.com/rocksaying/archives/2690905.html


--------------
相關文章:
Why does firebug add <tbody> to <table>?
http://stackoverflow.com/questions/1678494/why-does-firebug-add-tbody-to-table

沒有留言:

張貼留言

Facebook 留言板