此JavaScript代码使表具有响应性:
/* Credits:
https://gist.github.com/jpen365/f34bbff7d9db99f912e1e75193071718 */
var headertext = [];
var headers = document.querySelectorAll("thead");
var tablebody = document.querySelectorAll("tbody");
for (var i = 0; i < headers.length; i++) {
headertext[i]=[];
for (var j = 0, headrow; headrow = headers[i].rows[0].cells[j]; j++) {
var current = headrow;
headertext[i].push(current.textContent);
}
}
for (var h = 0, tbody; tbody = tablebody[h]; h++) {
for (var i = 0, row; row = tbody.rows[i]; i++) {
for (var j = 0, col; col = row.cells[j]; j++) {
col.setAttribute("data-th", headertext[h][j]);
}
}
}
我的问题是如何使用类is-style-stripes
的所有表来排除表,例如以下表:
<table class="wp-block-table aligncenter is-style-stripes">
<tbody>
<tr>
<th>Company</th><td>Amazon</td>
</tr>
<tr>
<th>Company</th><td>Google</td>
</tr>
</tbody>
</table>
答案 0 :(得分:1)
如果要获取所有不带类的表,则可以使用
document.querySelectorAll("table:not(.is-style-stripes)")
您将获得所有没有该类的表
如果要使用此类的表,请使用
document.querySelectorAll("table.is-style-stripes")
然后,您可以像搜索tbody
或thead
table.querySelectorAll("tbody")