Html 我们可以在每个 <tr> 上方的 table 中添加 div 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23440362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:41:54  来源:igfitidea点击:

Can we add div inside table above every <tr>?

html

提问by Amit

Hi am trying to add a div above every <tr>but when i look at the html console the div are showing outside the table. below is the html code.

嗨,我正在尝试在每个上方添加一个 div,<tr>但是当我查看 html 控制台时,该 div 显示在表格外。下面是html代码。

<table>
  <div>
    <tr><td></td></tr>
  </div>
  <div>
    <tr><td></td></tr>
  </div>
</table>

Is this not allowed? any help would be great.

这是不允许的吗?任何帮助都会很棒。

回答by Aditya Ekbote

"div"tag can not be used above "tr"tag. Instead you can use "tbody"tag to do your work. If you are planning to give id attribute to div tag and doing some processing, same purpose you can achieve through "tbody"tag. Divand Tableare both block level elements. so they can not be nested. For further information visit this page

“div”标签不能在“tr”标签上方使用。相反,您可以使用“tbody”标签来完成您的工作。如果您打算将 id 属性赋予 div 标签并进行一些处理,您可以通过“tbody”标签实现相同的目的。DivTable都是块级元素。所以它们不能嵌套。欲了解更多信息,请访问此页面

For example:

例如:

<table>
    <tbody class="green">
        <tr>
            <td>Data</td>
        </tr>
    </tbody>
    <tbody class="blue">
        <tr>
            <td>Data</td>
        </tr>
    </tbody>
</table>

secondly, you can put "div"tag inside "td"tag.

其次,您可以将“div”标签放在“td”标签中。

<table>
    <tr>
        <td>
            <div></div>
        </td>
    </tr>
</table>

Further questions are always welcome.

随时欢迎进一步提问。

回答by Patrick

No, you cannot insert a div directly inside of a table. It is not correct html, and will result in unexpected output.

不,您不能直接在表格中插入 div。它不是正确的 html,会导致意外输出。

I would be happy to be more insightful, but you haven't said what you are attempting, so I can't really offer an alternative.

我很乐意提供更深入的见解,但是您还没有说出您要尝试的内容,因此我无法真正提供替代方案。

回答by Deep Gandhi

You can not use tag to make group of more than one tag. If you want to make group of tag for any purpose like in ajax to change particular group or in CSS to change style of particular tag etc. then use

您不能使用标签将多个标签组合在一起。如果你想为任何目的创建一组标签,比如在 ajax 中更改特定组或在 CSS 中更改特定标签的样式等,然后使用

Ex.

前任。

<table>
  <tbody id="foods">
    <tr>
      <td>Group 1</td>
    </tr>
    <tr>
      <td>Group 1</td>
    </tr>
  </tbody>

  <tbody id="drinks">
    <tr>
      <td>Group 2</td>
    </tr>
    <tr>
      <td>Group 2</td>
    </tr>
  </tbody>
</table>

回答by Neel

You can't put a div directly inside a table but you can put div inside tdor thelement.

您不能将 div 直接放在表格中,但可以将 div 放在tdth元素中。

For that you need to do is make sure the div is inside an actual table cell, a tdor thelement, so do that:

为此,您需要做的是确保 div 位于实际的表格单元格、tdth元素内,因此请执行以下操作:

HTML:-

HTML:-

<tr>
  <td>
    <div>
      <p>I'm text in a div.</p>
    </div>
  </td>
</tr>

For more information :-

想要查询更多的信息 :-

http://css-tricks.com/using-divs-inside-tables/

http://css-tricks.com/using-divs-inside-tables/

回答by PAVAN BANSAL

In the html tables, <table>tag expect <tr>tag right after itself and <tr>tag expect <td>tag right after itself. So if you want to put a div in table, you can put it in between <td>and </td>tags as data.

在 html 表中,<table>标签 expect <tr>tag 紧随其后,<tr>标签 expect <td>tag 紧随其后。所以如果你想把一个div放在表格中,你可以把它放在<td></td>标签之间作为数据。

<table>
  <tr>
    <td>
      <div>
        <p>It works well</p>
      </div>
     </td>
  </tr>
<table>

回答by Jaro

You could use display: table-row-groupfor your div.

您可以display: table-row-group用于您的 div。

<table>
  <div style="display: table-row-group">
    <tr><td></td></tr>
  </div>
  <div style="display: table-row-group">
    <tr><td></td></tr>
  </div>
</table>

回答by semuzaboi

If we follow the w3 org table reference,and follow the Permitted Contentssection, we can see that the tabletags takes tbody(optional) and tras the only permitted contents.

如果我们遵循w3 org table reference并遵循Permitted Contents部分,我们可以看到table标签采用tbody(可选)和tr作为唯一允许的内容。

So i reckon it is safe to say we cannot add a divtag which is a flow content as a direct child of the tablewhich i understand is what you meant when you had said above a tr.

所以我认为可以肯定地说,我们不能添加一个div作为流内容的标签,table因为我理解这就是你在上面所说的意思tr

Having said that , as we follow the above link , you will find that it is safe to use divs inside the tdelement as seen here

话虽如此,当我们按照上面的链接进行操作时,您会发现divtd元素内使用s是安全的,如下所示