Is it really bad idea to group tr tags with div?

孤街醉人 提交于 2019-12-19 05:08:38

问题


I'm developing application with Backbone.js View class returns one element after rendering. It's ok if I use divs or spans. But problem starts when I start to render objects as tr rows. One objects should be rendered to 2-3 rows. So can I use this structure?

<table>
    <div>
        <tr>...</tr>
        <tr>...</tr>

    </div>
</table>

回答1:


divs immediately inside a table tag is invalid. use tbody instead




回答2:


Yes, it is a very bad idea.

The HTML specification does not allow div elements to be child elements of a table element, so it triggers error recovery routines in browsers.

Given this input:

<table>
  <tr><td>1</td></tr>
<div>
  <tr><td>2</td></tr>
  <tr><td>3</td></tr>
</div>
  <tr><td>4</td></tr>
<div>
  <tr><td>5</td></tr>
  <tr><td>6</td></tr>
</div>

The DOM that Firefox will generate (and you can see this by using the Inspect Element feature) will look like this:

<div>
  </div><div>
  </div><table>
  <tbody><tr><td>1</td></tr>
<tr><td>2</td></tr>
  <tr><td>3</td></tr>

  <tr><td>4</td></tr>
<tr><td>5</td></tr>
  <tr><td>6</td></tr>

</tbody></table>

Note that the div elements have been removed from the table and placed before it. This makes them useless for manipulating the rows.

HTML provides the thead, tfoot and tbody elements to group table rows. Use the appropriate ones of those instead of div elements (you can have only one thead and only one tfoot, but there are no limits on the number of tbody elements).




回答3:


That is not valid HTML. You can not nest block or inline elements in a table, only table elements, such as <tbody>, <tr> or <thead>. You can of course nest a <div> in a table cell (<td> or <th>).



来源:https://stackoverflow.com/questions/10258202/is-it-really-bad-idea-to-group-tr-tags-with-div

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