javascript - TD 中嵌入的表,COLSPAN 弄乱了父表

标签 javascript html html-table

我有一个表格,您可以在其中单击该行以查看更多详细信息。详细信息是使用 colspan 的嵌入式表。这应该占据整个宽度。最终发生的是嵌入式表格强制第一个列为嵌入式表格的宽度。

这是最初的样子。

NAME1 AMOUNT PURPOSE
NAME2 AMOUNT PURPOSE
NAME3 AMOUNT PURPOSE

Now click on the first row, and I expect to see the following where the embedded table takes up all the columns.

NAME1 AMOUNT PURPOSE
 VENDOR KIND AMOUNT DETAIL
 VENDOR KIND AMOUNT DETAIL
 VENDOR KIND AMOUNT DETAIL
NAME2 AMOUNT PURPOSE
NAME3 AMOUNT PURPOSE

This is what I am seeing in both chromium and firefox.

NAME1                       AMOUNT PURPOSE
 VENDOR KIND AMOUNT DETAIL
 VENDOR KIND AMOUNT DETAIL
 VENDOR KIND AMOUNT DETAIL
NAME2                       AMOUNT PURPOSE
NAME3                       AMOUNT PURPOSE

I could really use some help in trying to have the first column keep its width when the embedded table is shown. If you remove the style='display:none', it initially displays as expected. However, once you cycle through the show/hide toggle it displays incorrectly. Here is a sample.

<!DOCTYPE html>
<html><head>
<style>
.hidden { display: none; }
.money  { text-align: right; }
td {
padding-left:1em;
}
</style>
<script language="javascript">
function show_hide(elmid){
elm = document.getElementById(elmid);
if (elm.style.display=="none") {
    elm.style.display = "inline";
}
else {
    elm.style.display = "none";
}
}
</script>
</head>
<body>
<h1>Reimbursement</h1>
<p>Click on 'D. Smith' or anywhere in the first row to see detail.</p>
<table>
<tr><th>Who</th><th>Amount</th><th>Purpose</th></tr>
<tr onclick="show_hide('exp47')"><td>D. Smith</td><td class='money'>37.61</td><td>Treats &amp; Snacks</td></tr>
<tr id='exp47' style='display:none'>
  <td colspan='3'>
  <table><thead>
    <tr><th>Vendor</th><th>Kind</th><th>Amount</th><th>Detail</th></tr>
  </thead><tbody>
    <tr><td>Dollar Tree</td><td>food</td><td>3.00</td><td>teddy bear cookies [3]</td></tr>
    <tr><td>Dollar Tree</td><td>tax</td><td>0.05</td><td>1.50%</td></tr>
    <tr><td>Sams</td><td>supplies</td><td>10.07</td><td>Lysol 3 pac</td></tr>
    <tr><td>Sams</td><td>supplies</td><td>6.98</td><td>hand sani</td></tr>
    <tr><td>Walmart</td><td>food</td><td>6.98</td><td>pf bulk gf</td></tr>
    <tr><td>Walmart</td><td>food</td><td>3.58</td><td>gm cereal</td></tr>
    <tr><td>Walmart</td><td>food</td><td>2.42</td><td>gv h4y rice</td></tr>
    <tr><td>Walmart</td><td>food</td><td>4.44</td><td>gv ff sticks [3]</td></tr>
    <tr><td>Walmart</td><td>tax</td><td>0.30</td><td>1.75%</td></tr>
    <tr><td>Jewel</td><td>food</td><td>6.00</td><td>glbl cookies 10z [6]</td></tr>
    <tr><td>Jewel</td><td>tax</td><td>0.14</td><td>2.25%</td></tr>
  </tbody></table>
  </td>
</tr>
<tr><td>Another</td><td class='money'>?.??</td><td>Another</td></tr>
</table></body></html>

最佳答案

在你的 javascript 中:

elm.style.display = "inline";

内联显示是 tr - table row - 标签的禁忌。

正确的做法是将显示设置为“table-row”:

elm.style.display = "table-row";

您可以在这里阅读更多相关信息:http://www.quirksmode.org/css/display.html 顺便说一下,您应该考虑为这类问题添加一个 jsFiddle。

关于javascript - TD 中嵌入的表,COLSPAN 弄乱了父表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13670542/

相关文章:

javascript - 使用 jQuery 和 Laravel append json 数据的更有效方法

javascript - 如何基于 Promise 制作异步函数

html - 在这种情况下如何订购 z-index

android - 元素在移动设备上的大小调整不均

Javascript对象原型(prototype)错误

javascript - 使用 d3.js 和 topoJSON 创建非洲 map

html - 有序列表两列流

javascript - 如何与推特分享textarea内容?

css - 当分辨率低于 768px 时,bootstrap 3 中的响应表加入

javascript - 单击按钮并使用 JavaScript(或 jquery)获取同一行中单元格的值