表格的格式不如预期那样。

huangapple go评论51阅读模式
英文:

Table doesn't format as expected

问题

I have a table that displays as I wish, however I added a button below so the user can expand and show a secondary table. However, this second table does not display like the first one and I wanted to know why that is. The second table displays to the left with no spacing between the boxes as expected.

第一个表格(显示正确):

<div id="expenses-table">
  <table class="table">
    <tr>
      <th>日期</th>
      <th>供应商</th>
      <th>金额</th>
      <th>原因</th>
    </tr>
    <tr>
      <th><input type="text"></th>
      <th><input type="text"></th>
      <th><input type="text"></th>
      <th><input type="text"></th>
    </tr>
  </table>
  
  <br>
  <br>
</div>

问题中的第二个表格不显示正确:

.table,
tr {
  border: 1px solid black;
  width: 80%;
  margin-left: auto;
  margin-right: auto;
  border-radius: 5px;
}
<table id="expand" style="display:none" class="table">
  <tr>
    <th>日期</th>
    <th>供应商</th>
    <th>金额</th>
    <th>原因</th>
  </tr>
  <!-- 这里有多个重复的行 -->
</table>
<button id="expand-btn" class="button" onClick="expandTable()">展开</button>

如有其他翻译需求,请告诉我。

英文:

I have a table that displays as I wish, however I added a button below so the user can expand and show a secondary table. However, this second table does not display like the first one and I wanted to know why that is. The second table displays to the left with no spacing between the boxes as expected

First table (displays correctly)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;div id=&quot;expenses-table&quot;&gt;
  &lt;table class=&quot;table&quot;&gt;
    &lt;tr&gt;
      &lt;th&gt;Date&lt;/th&gt;
      &lt;th&gt;Supplier&lt;/th&gt;
      &lt;th&gt;Amount&lt;/th&gt;
      &lt;th&gt;Reason&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
      &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
      &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
      &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;br&gt;
  &lt;br&gt;

<!-- end snippet -->

Second table in question, doesn't display like the first one

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.table,
tr {
  border: 1px solid black;
  width: 80%;
  margin-left: auto;
  margin-right: auto;
  border-radius: 5px;
}

<!-- language: lang-html -->

&lt;table id=&quot;expand&quot; style=&quot;display:none&quot; class=&quot;table&quot;&gt;
  &lt;tr&gt;
    &lt;th&gt;Date&lt;/th&gt;
    &lt;th&gt;Supplier&lt;/th&gt;
    &lt;th&gt;Amount&lt;/th&gt;
    &lt;th&gt;Reason&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;/table&gt;
&lt;/div&gt;

&lt;button id=&quot;expand-btn&quot; class=&quot;button&quot; onClick=&quot;expandTable()&quot;&gt;Expand&lt;/button&gt;

<!-- end snippet -->

答案1

得分: 1

你的要求已经翻译好了,以下是你提供的内容:

"Again! change the th tag to td for all table cells except the for header"

以及代码部分。

英文:

As you're not responding to comments or developing further more your question as requested (which is exactly the opposite of what you're required to do when asking). Imma try to guess your final goal and present the following.

Again! change the th tag to td for all table cells except the for header

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function expandTable() {
  document.getElementById(&quot;expand&quot;).classList.toggle(&#39;hidden&#39;);
}

<!-- language: lang-css -->

.table,
tr {
  border: 1px solid black;
  width: 80%;
  margin-left: auto;
  margin-right: auto;
  border-radius: 5px;
}

.hidden {
  display: none;
}

<!-- language: lang-html -->

&lt;div id=&quot;expenses-table&quot;&gt;
  &lt;table class=&quot;table mb-4&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Date&lt;/th&gt;
      &lt;th&gt;Supplier&lt;/th&gt;
      &lt;th&gt;Amount&lt;/th&gt;
      &lt;th&gt;Reason&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
      &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
      &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
      &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;tbody id=&quot;expand&quot; class=&quot;hidden&quot;&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
    &lt;th&gt;&lt;input type=&quot;text&quot;&gt;&lt;/th&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;

&lt;button id=&quot;expand-btn&quot; class=&quot;button&quot; onClick=&quot;expandTable()&quot;&gt;Expand&lt;/button&gt;

<!-- end snippet -->

答案2

得分: -1

如果需要在另一个表格内包裹一个表格,您需要像这样操作(将<table>嵌套在<table>标记内):

&lt;table&gt;
  &lt;tr&gt;
    &lt;td&gt;
      &lt;table&gt;
         &lt;tr&gt;
            &lt;td&gt;&lt;/td&gt;
         &lt;/tr&gt;
      &lt;/table&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
&lt;table&gt;

此外,CSS 无法很好地控制表格单元格。当内容较长时,表格将自动扩展,就像这样:

表格的格式不如预期那样。

表格的格式不如预期那样。

因此,如果您真的需要控制样式,请使用<div>而不是<table>。

英文:

If you need to wrap a table inside another table, you need to do something like this (nest a < table > inside a < table > tag):

&lt;table&gt;
  &lt;tr&gt;
    &lt;td&gt;
        &lt;table&gt;
           &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
           &lt;/tr&gt;
        &lt;/table&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
&lt;table&gt;

In addition, CSS cannot control table cell well. When content are long, table will expend by itself, like this:
表格的格式不如预期那样。

表格的格式不如预期那样。

Therefore, use < div > instead of < table > if you really need control the style.

huangapple
  • 本文由 发表于 2023年4月13日 23:25:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定