Html 一个人可以在另一个人里面吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3953144/
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 04:52:02  来源:igfitidea点击:

Can a tbody be inside another one?

html

提问by Michael Madsen

Is it possible to have an inner tbody inside an outer tbody like this:

是否可以像这样在外部 tbody 内部有一个内部 tbody:

here is a sample css:

这是一个示例css:

  <style type="text/css">
    .class1 {background-color:#ff0000;}
    .class2 {background-color:#00ff00;}
  </style>

Here is the sample HTML

这是示例 HTML

<table>
  <tbody id="outer" class="class1">
    <tr>
      <td>...</td>
      <td>...</td>
    </tr>
    <tbody id="inner" class="class2">
      <tr>
        <td>...</td>
        <td>...</td>
      </tr>
    </tbody> <!-- inner -->
    <tr>
      <td>...</td>
      <td>...</td>
    </tr>
  </tbody> <!-- outer -->
</table>

My purpose is to apply class1 CSS to the outer tbody and class2 to the inner tbody. But the last TR is consider to be out of the outer tbody as I want it to be inside the outer tbody.

我的目的是将 class1 CSS 应用于外部 tbody,将 class2 应用于内部 tbody。但是最后一个 TR 被认为是在外部 tbody 之外,因为我希望它在外部 tbody 内部。

How can i do that?

我怎样才能做到这一点?

回答by Michael Madsen

While this may work in practice, it is not legal HTML.

虽然这在实践中可能有效,但它不是合法的 HTML

However, you are allowed to have multiple TBODY elements in a single TABLE element, so you could do this:

但是,您可以在单个 TABLE 元素中包含多个 TBODY 元素,因此您可以这样做:

<table> 
  <tbody class="show"> 
    <tr> 
      <td>...</td> 
      <td>...</td> 
    </tr> 
  </tbody>
  <tbody class="hide"> 
    <tr> 
      <td>...</td> 
      <td>...</td> 
    </tr> 
  </tbody>
  <tbody class="show"> 
    <tr> 
      <td>...</td> 
      <td>...</td> 
    </tr> 
  </tbody>
</table> 

Alternatively, you may be able to nest tables, although I wouldn't really recommend that.

或者,您也可以嵌套表,尽管我真的不建议这样做。

回答by Sarfraz

The way would be to create another table and use tbodyafterwards:

方法是创建另一个表并在tbody之后使用:

<table>
  <tbody id="outer" class="show">
    <tr>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <td colspan="2">
         <table>
        <tbody id="inner" class="hide">
         <tr>
           <td>...</td>
           <td>...</td>
         </tr>
       </tbody> <!-- inner -->
         </table>
      </td>
    </tr>
    <tr>
      <td>...</td>
      <td>...</td>
    </tr>
  </tbody> <!-- outer -->
</table>

回答by Pekka

Nope, this is not possible. According to the spec, the TBODYelement can contain only TRelements.

不,这是不可能的。根据规范,TBODY元素只能包含TR元素。

<!ELEMENT TBODY    O O (TR)+           -- table body -->

what do you want to achieve?

你想达到什么目的?

回答by Toto

I'm afraid you can't do such thing.

恐怕你不能做这种事。

According to W3Ca tbody can't inside another one.

根据W3C,一个 tbody 不能在另一个里面。

回答by Nikita Rybak

Not likely. If all you want is to 'distinguish' some set of rows, I would assign class innerto each row in question (instead of tbody). Then you easily manipulate those rows with any js framework, like $('tr.inner').show();.

不见得。如果您只想“区分”某些行集,我会将类分配inner给相关的每一行(而不是tbody)。然后,您可以使用任何 js 框架轻松操作这些行,例如$('tr.inner').show();.