gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:59:28 24 4
gpt4 key购买 nike

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

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 中,它被称为 with content: 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
-----------

更新试试这个代码:

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

这显示了精简模式下的情况


当您查看这两张图片时,您会发现标准表格格式使用的术语“预计到达日期”在第一个作品上大写。精简版使用“预计到达日期”,所有单词都大写。这是为了表明值(value)观来自不同的地方。

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

<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属性。

This does not work if you try to use <table>, <tr>, <th> and <td> tags.

关于javascript - 将内容拉入 td 数据标签属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47101366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com