2017-06-12 48 views
1

大家好! 我在許多文章和課程中看到表格元素中的<tfoot>應放置在<tbody>元素之前。正確...但是,當我這樣做時,驗證程序(https://validator.w3.org)告訴我這是錯誤的,並且在此情況下我不能使用<tfoot>。所以我決定在<tbody>之後加上<tfoot>,驗證器在我的代碼中沒有發現任何錯誤。 那麼......無論如何,正確的做法是什麼? 我還發現,似乎與其自己的規格:https://www.w3.org/TR/html51/tabular-data.html#the-tfoot-elementHTML5 - 2017正確的訂購方式<tbody>和<tfoot>元素?

回答

3

<tfoot> spec規則were changed in December 2015 to disallow <tfoot> before <tbody>

至於W3C規範去,去尋找相關要求的地方實際上是the part of the spec that states the rules for what is allowed in the <table> element,它說:

順序爲:可選的caption元素,其次是零個或多個colgroup元素隨後任選地跟隨元素,隨後是零個或多個元素或者一個或多個元素,隨後任選地與tfoot元素,任選地與一個或多個腳本支持元素混合。

通知,上面寫着tfoottbody後允許的,但它並沒有說這是tbody之前允許的。

+0

對於其他的信息(比如WordPress插件開發人員)在這裏結束了,驗證https://validator.w3.org/將着眼於網站的doctype,並重定向到https: //validator.w3.org/nu/ if <!DOCTYPE HTML>。這可能會令人困惑,因爲根據所使用的網站的文檔類型,相同的表tfoot html將是有效的或無效的。(顯然!)然而,似乎並不是所有文檔類型都有效的通用標準標準,也沒有辦法以確定所使用的doctype,據我所知,在生成表html之前。 – anmari

0

A <table>元件。 <tfoot>必須出現在任何<caption>, <colgroup>, <thead>, <tbody>, or <tr>元素之後。請注意,這是HTML5的要求。

元素不能放在任何<tbody><tr>元素之後。請注意,這與HTML5的上述規範要求直接抵觸。

<table border="1" align="center"> 
 
    <caption>A table</caption> 
 
    <thead> 
 
    <tr> 
 
     <th colspan="3">Invoice #123456789</th> 
 
     <th>14 January 2025 
 
    </tr> 
 
    <tr> 
 
     <td colspan="2"> 
 
     <strong>Pay to:</strong><br> Acme Billing Co.<br> 123 Main St.<br> Cityville, NA 12345 
 
     </td> 
 
     <td colspan="2"> 
 
     <strong>Customer:</strong><br> John Smith<br> 321 Willow Way<br> Southeast Northwestershire, MA 54321 
 
     </td> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <th>Name/Description</th> 
 
     <th>Qty.</th> 
 
     <th>@</th> 
 
     <th>Cost</th> 
 
    </tr> 
 
    <tr> 
 
     <td>Paperclips</td> 
 
     <td>1000</td> 
 
     <td>0.01</td> 
 
     <td>10.00</td> 
 
    </tr> 
 
    <tr> 
 
     <td>Staples (box)</td> 
 
     <td>100</td> 
 
     <td>1.00</td> 
 
     <td>100.00</td> 
 
    </tr> 
 
    </tbody> 
 
    <tfoot> 
 
    <tr> 
 
     <th colspan="3">Subtotal</th> 
 
     <td> 110.00</td> 
 
    </tr> 
 
    <tr> 
 
     <th colspan="2">Tax</th> 
 
     <td> 8% </td> 
 
     <td>8.80</td> 
 
    </tr> 
 
    <tr> 
 
     <th colspan="3">Grand Total</th> 
 
     <td>$ 118.80</td> 
 
    </tr> 
 
    </tfoot> 
 
</table>