Is it necessary to have <tbody>
in every table? According to Standards.
Is it necessary to have in every table?
2019-02-07 17:51发布
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):问题:
回答1:
Only if you define thead
and tfoot
. It is mostly used when the table has multiple bodies of content. If the data in the table is easily understood to be the tbody
then you can safely omit it.
回答2:
For the small fraction of your users still using IE7, you MUST add encapsulate your tr's in a tbody tag if you're building a table with the DOM methods!
This will work in all major browsers:
var table = document.createElement('table');
var tbody = document.createElement('tbody');
var tr = document.createElement('tr');
tbody.appendChild(tr);
table.appendChild(tbody);
This will NOT work in IE7:
var table = document.createElement('table');
var tr = document.createElement('tr');
table.appendChild(tr);
A quick blog post of mine on building tables:
http://blog.svidgen.com/2012/05/building-tables-in-ie7-with-javascript.html
It may be notable that I no longer make the effort to support IE7 on my own projects. The IE<=7 share is likely negligible for most sites at this point.
回答3:
Quoting the HTML 4 spec: "The TBODY start tag is always required except when the table contains only one table body and no table head or foot sections. The TBODY end tag may always be safely omitted."
So, you must have a <tbody>
tag if you have a <thead>
or <tfoot>
See also: MDN
回答4:
Most browsers are forgiving but even so I add the pair in all tables that I use now. Even trivial tables. Especially now that I'm using CSS more and more to decorate those tables.
All that being said I have old tables that still work fine on the newest browsers. I'm learning the hard way but taking the few extra Micro seconds to add the optional tags here and there ends up saving you money/time in the long run.
Dave
问题:
回答1:
Only if you define thead
and tfoot
. It is mostly used when the table has multiple bodies of content. If the data in the table is easily understood to be the tbody
then you can safely omit it.
回答2:
For the small fraction of your users still using IE7, you MUST add encapsulate your tr's in a tbody tag if you're building a table with the DOM methods!
This will work in all major browsers:
var table = document.createElement('table');
var tbody = document.createElement('tbody');
var tr = document.createElement('tr');
tbody.appendChild(tr);
table.appendChild(tbody);
This will NOT work in IE7:
var table = document.createElement('table');
var tr = document.createElement('tr');
table.appendChild(tr);
A quick blog post of mine on building tables:
http://blog.svidgen.com/2012/05/building-tables-in-ie7-with-javascript.html
It may be notable that I no longer make the effort to support IE7 on my own projects. The IE<=7 share is likely negligible for most sites at this point.
回答3:
Quoting the HTML 4 spec: "The TBODY start tag is always required except when the table contains only one table body and no table head or foot sections. The TBODY end tag may always be safely omitted."
So, you must have a <tbody>
tag if you have a <thead>
or <tfoot>
See also: MDN
回答4:
Most browsers are forgiving but even so I add the pair in all tables that I use now. Even trivial tables. Especially now that I'm using CSS more and more to decorate those tables.
All that being said I have old tables that still work fine on the newest browsers. I'm learning the hard way but taking the few extra Micro seconds to add the optional tags here and there ends up saving you money/time in the long run.
Dave