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

2023-12-04

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

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;
  }
}
<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>

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


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

| 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 |

对此?:

-----------
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
-----------

UPDATE试试这个代码:

function toggle() {
  var table = document.querySelector('.my-table');
  table.classList.toggle('show-thin');
}
.table {
  border-collapse: collapse;
  display: inline-table;
}

.tr {
  display: table-row;
}

.th, .td {
  display: table-cell;
  border: 1px solid #555;
  padding: 3px 6px;
}

.th {
  background-color: #ddd;
  font-weight: bold;
  text-align: center;
}

.td {
  text-align: right;
}

.my-table.show-thin {
  display: block;
}

.show-thin .tr {
  border-bottom: 1px solid black;
  display: block;
  margin-bottom: 2px;
  padding-bottom: 2px;
}

.show-thin .td {
  border: none;
  display: block;
  padding: 0;
  text-align: left;
}

.show-thin .td:before {
  content: attr(title) ':';
  display: inline-block;
  font-weight: bold;
  padding-right: 5px;
}

.show-thin .thin-hide {
  display: none;
}
<button onclick="toggle()">Toggle</button>
<hr/>
<div class="my-table">
<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>
<div class="tr">
  <span class="td" title="Account">1234</span>
  <span class="td" title="Estimated Arrival Date">03/15/2001</span>
  <span class="td" title="Amount">$1.00</span>
  <span class="td" title="Period">3rd</span>
</div>
<div class="tr">
  <span class="td" title="Account">1235</span>
  <span class="td" title="Estimated Arrival Date">04/21/2002</span>
  <span class="td" title="Amount">$12.00</span>
  <span class="td" title="Period">4th</span>
</div>
<div class="tr">
  <span class="td" title="Account">4594</span>
  <span class="td" title="Estimated Arrival Date">11/11/2011</span>
  <span class="td" title="Amount">$45.50</span>
  <span class="td" title="Period">2nd</span>
</div>
</div>

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

诀窍在于放置title每个单元格的属性。然后使用CSS来显示title当处于精简模式时。


Wide mode

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


Thin mode

这显示了瘦模式下的情况


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

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

<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>

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

如果您尝试使用,这不起作用<table>, <tr>, <th> and <td> tags.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将内容拉入 td 数据标签属性 的相关文章

随机推荐

  • R pheatmap:执行聚类并显示每个注释类别的树状图

    我知道如何使用 pheatmap 按注释类别对行 基因 进行分组 并且我知道如何对整组行 基因 执行 Person 的相关聚类 但我想要完成的是执行聚类 并显示独立的每个类别独立的树状图 这可能吗 或者我是否被迫为每个类别创建单独的热图以在
  • 4 个条件中的 2 个以上正则表达式

    我对用户密码有4个要求 至少 1 个 a z 字符 至少 1 个 A Z 字符 至少 1 个 0 9 字符 至少 1 个符号 但是 用户只需满足 4 个条件中的 2 个以上即可 密码VVVV1111 234567 AaAaAaAa or A
  • Python中的一等函数是什么

    我仍然对什么是一流函数感到困惑 如果我理解正确的话 一等函数应该使用一个函数作为对象 它是否正确 这是一流的功能吗 def this is example myarg1 return myarg1 def this is another e
  • 具有位置条件的 MongoDB 分页

    我想要获取按字段排序的数据 例如 db Users find limit 200 sort rating 1 skip 0 这是工作 我得到了排序的数据 并且可以使用分页 但是 如果添加条件 find location near 12 32
  • 将搜索文本字段实现到 jTable 中

    现在我有一个使用 KeyReleased 事件实现的搜索文本字段 当我开始输入例如 Andrew 中的 An 时 它没有找到 更新 jTable 并且在我完成准确输入 区分大小写 我想要的名称之后寻找 所以 我想要的是从此实现 Filter
  • 有没有办法在 Perl 的文件读取器中指定行标记分隔符?

    我正在 Perl 中通过 CGI 读取文本文件 并注意到当文件保存在 mac 的 textEdit 中时 行分隔符可以被识别 但是当我上传直接从 Excel 导出的 CSV 时 它们不能被识别 我猜这是一个 n 与 r 问题 但这让我想到
  • Mercurial 每种文件类型的合并策略

    All 我想使用 kdiff 合并具有特定后缀 例如 c h 的所有文件 并且我想对具有另一个后缀 例如 c h 的所有文件做两件事 关闭预合并并使用内部 其他 mdl 这样做的目的是允许我对特定文件类型使用一种 破坏合并 即 不可合并的文
  • 使用 javascript 将文件附加到 input type="file"

    我需要将 JavaScript 生成的文件 最多几 MB 发送到服务器 我需要用 POST 不是 ajax POST 来完成 如何通过 JavaScript 将文件添加到 input type file 根据预填充 HTML 表单文件输入由
  • 无法在 JavaScript 中启用按钮

    我也尝试过这个 但仍然不起作用 find set enabled true
  • 清单合并失败:uses-sdk:minSdkVersion 8 不能小于库中声明的版本 9 [com.google.android.gms:play-services:7.8.0]

    问题是 错误 任务 app processDebugManifest 执行失败 清单合并失败 uses sdk minSdkVersion 8 不能小于库 com google android gms play services 7 8 0
  • 优化的合并排序比快速排序更快

    http jsperf com optimized mergesort versus quicksort 为什么这种半缓冲区合并排序的速度与快速排序一样快 快速排序是 虽然它占用了原地log n 递归 堆栈空间 缓存友好 这个半缓冲区合并排
  • belongs_to 和 has_one 之间有什么区别?

    和有什么区别belongs to and a has one 阅读 Ruby on Rails 指南对我没有帮助 他们本质上做同样的事情 唯一的区别是你站在关系的哪一边 如果一个User has a Profile 然后在User你会上的课
  • 带三角形条的圆圈

    我一直在尝试在 OpenGL 中创建一个圆圈 但我不能使用三角形扇形 因为我读到它们在 directx 中不再可用 而且我还将进行 directx 调用 我不太明白三角形带是如何工作的 我所有的实现都有漏洞或奇怪的怪癖 任何人都可以帮助我
  • Risc-V:能够利用 GCC 的简单 RV32I 实现的最低 CSR 要求

    对于能够运行 GCC 生成的机器代码的 RV32I 最低的 CSR 要求是什么 我正在考虑一个简单的基于 fpga 嵌入式 的实现 不需要虚拟内存或 Linux 支持 另外 我应该使用什么 GCC 标志来防止它使用未实现的 CSR 相关指令
  • 用下划线替换空格,但不是全部

    我对 Python 还很陌生 但我们正在清理一些文本文件 除此之外 我还需要执行以下操作 用下划线替换空格 但仅限于某些情况 这些情况的开头标记为 2 末尾标记为 1 E g Here is some text 2This is an ex
  • Python-计算不同组中的范围(最高-最低)

    我已经对我的数据进行了分组 现在 我想做的是每周从 高 列中选择最高值 并从 低 列中选择最低值 然后用最高值减去最低值来获得范围 但代码总是错误的 有人给我一个主意吗 这是我的数据框的一部分 和我的错误代码 grouped df grou
  • 根据先前查询的结果更新表

    如何根据上一个查询的结果更新表 The 原始查询 非常感谢GMB 可以找到地址 users 表 中与地址 address effect 表 匹配的任何项目 从这个查询的结果中 我想找到address effect表中的地址计数 并将其添加到
  • 如何通过 Windows 命令行确定 ALBD 服务是否正在运行?

    好的 我知道如何启动和停止 ALBD 但是如何确定它当前是否正在运行 我想将其放入 Perl 或 DOS 脚本中 因此它必须是非 GUI 解决方案 请参阅此技术说明 从 Windows 命令行启动和停止 ClearCase 并注意albd不
  • 在PRG模式中如何删除刷新成功页面上的ActionMessage

    为了避免表单重新提交 我使用了 POST Redirect GET 模式 它工作正常 现在 在我的注册页面 成功页面 上 它显示 ActionMessage 审阅已成功插入 当用户刷新页面时我想删除此消息 我在JSP页面上的代码
  • 将内容拉入 td 数据标签属性

    我使用的是响应式表格样式 该样式将在较小的屏幕尺寸下折叠并在每个单元格之前显示表格标题 body font family Open Sans sans serif line height 1 25 table border 1px soli