将内容拉入 td 数据标签属性

kfi*_*vel 2 html javascript css html-table responsive

我使用的是响应式表格样式,该样式将在较小的屏幕尺寸下折叠并在每个单元格之前显示表格标题。

body {
  font-family: "Open Sans", sans-serif;
  line-height: 1.25;
}
table {
  border: 1px solid #ccc;
  border-collapse: collapse;
  margin: 0;
  padding: 0;
  width: 100%;
  table-layout: fixed;
}
table caption {
  font-size: 1.5em;
  margin: .5em 0 .75em;
}
table tr {
  background: #f8f8f8;
  border: 1px solid #ddd;
  padding: .35em;
}
table th,
table td {
  padding: .625em;
  text-align: center;
}
table th {
  font-size: .85em;
  letter-spacing: .1em;
  text-transform: uppercase;
}
@media screen and (max-width: 600px) {
  table {
    border: 0;
  }
  table caption {
    font-size: 1.3em;
  }
  table thead {
    border: none;
    clip: rect(0 0 0 0);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
    color: red;
    background-color:#000;
  }
  table tr {
    border-bottom: 3px solid #ddd;
    display: block;
    margin-bottom: .625em;
  }
  table td {
    border-bottom: 1px solid #ddd;
    display: block;
    font-size: .8em;
    text-align: right;
  }
  table td:before {
    /*
    * aria-label has no advantage, it won't be read inside a table
    content: attr(aria-label);
    */
    content: attr(data-label);
    float: left;
    font-weight: bold;
    text-transform: uppercase;
  }
  table td:last-child {
    border-bottom: 0;
  }
  table td:first-child{
    color:white;
    background: #000;
  }
}
Run Code Online (Sandbox Code Playgroud)
<table>
  <caption>Statement Summary</caption>
  <thead>
    <tr>
      <th scope="col">Account</th>
      <th scope="col">Estimated arrival date</th>
      <th scope="col">Amount</th>
      <th scope="col">Period</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td data-label="Account">Visa - 3412</td>
      <td data-label="Really freaking long div magic">04/01/2016</td>
      <td data-label="Amount">$1,190</td>
      <td data-label="Period">03/01/2016 - 03/31/2016</td>
    </tr>
    <tr>
      <td scope="row" data-label="Account">Visa - 6076</td>
      <td data-label="Due Date">03/01/2016</td>
      <td data-label="Amount">$2,443</td>
      <td data-label="Period">02/01/2016 - 02/29/2016</td>
    </tr>
    <tr>
      <td scope="row" data-label="Account">Corporate AMEX</td>
      <td data-label="Due Date">03/01/2016</td>
      <td data-label="Amount">$1,181</td>
      <td data-label="Period">02/01/2016 - 02/29/2016</td>
    </tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

列标题使用每个相应表格单元格上的数据标签属性来表示。在 CSS 中,它的调用内容为:attr(data-label)。我将此样式应用于一些相当大的表格,并且我不想为 HTML 中的每个单元格编写数据标签。有没有办法使用 Javascript 将 th 拉入数据标签属性?

Int*_*lia 5

您希望表格从以下位置开始吗:

| Account | Estimated arrival date | Amount | Period |
| ------- | ---------------------- | ------ | ------ |
|    1234 |             03/15/2001 |  $1.00 |    3rd |
|    1235 |             04/21/2002 | $12.00 |    4th |
|    4594 |             11/11/2011 | $45.00 |    2nd |
Run Code Online (Sandbox Code Playgroud)

对此?:

-----------
Account: 1234
Estimated Arrival Date: 03/15/2001
Amount: $1.00
Period: 3rd
-----------
Account: 1235
Estimated Arrival Date: 04/21/2002
Amount: $12.00
Period: 4th
-----------
Account: 4594
Estimated Arrival Date: 11/11/2011
Amount: $45.00
Period: 2nd
-----------
Run Code Online (Sandbox Code Playgroud)

更新试试 这个代码:

| Account | Estimated arrival date | Amount | Period |
| ------- | ---------------------- | ------ | ------ |
|    1234 |             03/15/2001 |  $1.00 |    3rd |
|    1235 |             04/21/2002 | $12.00 |    4th |
|    4594 |             11/11/2011 | $45.00 |    2nd |
Run Code Online (Sandbox Code Playgroud)
-----------
Account: 1234
Estimated Arrival Date: 03/15/2001
Amount: $1.00
Period: 3rd
-----------
Account: 1235
Estimated Arrival Date: 04/21/2002
Amount: $12.00
Period: 4th
-----------
Account: 4594
Estimated Arrival Date: 11/11/2011
Amount: $45.00
Period: 2nd
-----------
Run Code Online (Sandbox Code Playgroud)
function toggle() {
  var table = document.querySelector('.my-table');
  table.classList.toggle('show-thin');
}
Run Code Online (Sandbox Code Playgroud)

我的示例使用一个类将值从表格格式更改为行格式。但也可以使用媒体查询来完成。这更容易演示。

诀窍在于将title属性放置在每个单元格上。然后使用 CSS 来显示title处于瘦模式时的情况。


宽屏模式

这显示了表格在宽模式下的样子


瘦身模式

这显示了瘦模式下的情况


当您查看这两个图像时,您会发现标准表格格式使用术语“预计到达日期”,并且第一个作品大写。精简版本使用“预计到达日期”,所有单词均大写。这是为了表明这些值来自不同的地方。

在宽模式下,标题来自这里:

<div class="tr thin-hide">
  <span class="th">Account</span>
  <span class="th">Estimated arrival date</span>
  <span class="th">Amount</span>
  <span class="th">Period</span>
</div>
Run Code Online (Sandbox Code Playgroud)

在精简模式下,它来自title属性。

<table>如果您尝试使用、<tr>和标签,<th>这将不起作用。<td>