0
votes

When I look at W3schoool the initial tutorial or examples don't use thead or tbody but I have jQuery code that looks like this:

$("#mytable tbody tr:nth-child(odd)").addClass("oddColor");

or

$("#mytable tr:nth-child(odd)").addClass("oddColor");

and my table looks like this (notice no thead or tbody tags)

<table>
  <tr><th>Col1</th><th>Col2</th></tr>

  <tr><td>Row 1</td><td>Row 1</td></tr>
  <tr><td>Row 2</td><td>Row 2</td></tr>
  <tr><td>Row 3</td><td>Row 3</td></tr>
  <tr><td>Row 4</td><td>Row 4</td></tr>
</table>

does jQuery count the "th" row as tbody? and if the browsers automatically add in the tbody why do they not put it on the "td" row (instead of the "th" row) It looks like it does but i would have thought that it would look for the first "td" row.

Obviously by adding thead and tbody on all tables makes it 100% explicit but I was just confused by what jQuery does when those tags are not there and why it behaves like that.

Maybe a better question is if browsers add in tbody, why would they not add the tbody on the td row instead of the th row?

2
Why would you not just add them yourself?Troy Cosentino
I added that as part of the question. i was more curious about the browser behaviorleora
What if a row contained some th elements and some td elements. What would the browser do then?Alohci

2 Answers

2
votes

tbody is created automatically, and will wrap all of the table rows, if no tbody or thead is explicitly used. th is allowed within body rows, there's no "inspection" of the rows' contents to see which rows should be head or body rows.

If you want a thead, with rows excluded from tbody, you must add it explicitly - at which point you'll also need to add tbody, too.

See Why do browsers insert tbody element into table elements?

0
votes

In RFC1942 (HTML Tables), from 1996, Dave Raggett says

Tables may be divided up into head and body sections. The THEAD and TFOOT elements are optional, but one or more TBODY elements are always required. If the table only consists of a TBODY section, the TBODY start and end tags may be omitted, as the parser can infer them. If a THEAD element is present, the THEAD start tag is required, but the end tag can be omitted, provided a TFOOT or TBODY start tag follows. The same applies to TFOOT.

Note: This definition provides compatibility with tables created for the older model, as well as allowing the end tags for THEAD, TFOOT and TBODY to be omitted. (my emphasis)

The THEAD, TFOOT and TBODY elements provide a convenient means for controlling rendering. If the table has a large number of rows in the body, user agents may choose to use a scrolling region for the table body sections. When rendering to a paged device, tables will often have to be broken across page boundaries. The THEAD, TFOOT and TBODY elements allow the user agent to repeat the table foot at the bottom of the current page, and then the table head at the top of the new page before continuing on with the table body.

The "older model" appears in HTML 3.0. Although HTML 3.0 was abandoned before completion in favour of HTML 3.2, it seems that at least some tables on the web were created during that period. It says:

Content Model: Optional CAPTION, then one or more table rows (TR)

I.e. There were no TBODY, THEAD or TFOOT elements at that time.

So the assumption would be that those tables created at that time would expect all rows, regardless of whether they contained TH elements or TD elements to have behaviour like that of TBODY, e.g. the rows would not repeated at the top and bottom of each printed page as was expected pf THEAD and TFOOT respectively.

So for backward compatibility only wrapping with TBODY is consistent with the original uses of HTML tables.