2 回答
TA贡献1898条经验 获得超8个赞
您刚刚在 Javascript 代码中使用了第一个 tbody。让我们试试这个。
var tbody = document.querySelectorAll('tbody');
tbody.forEach(function(bdy){
let thscoped = bdy.querySelector('.sem');
var count = 0;
for (var ch = bdy.firstChild; ch; ch = ch.nextSibling)
if (ch instanceof HTMLElement) count++;
thscoped.setAttribute("rowspan", count);
});
.sem {
border:1px solid red
}
<div class="table-responsive">
<table class="responsive-table table-bordered ">
<thead>
<tr>
<th scope="col">Year</th>
<th scope="col">Short</th>
<th scope="col">Exam</th>
<th scope="col">P</th>
<th scope="col">S</th>
<th scope="col">A</th>
<th scope="col">SJ</th>
<th scope="col">TJ</th>
<th scope="col">PK</th>
<th scope="col">ECTS</th>
</tr>
</thead>
<tbody>
<tr>
<th class="sem" scope="row">1</th>
<td data-title="Short">Lor</td>
<td data-title="Exam">Lorem</td>
<td data-title="P">4</td>
<td data-title="S">4</td>
<td data-title="A">0</td>
<td data-title="SJ">0</td>
<td data-title="TJ">0</td>
<td data-title="PK">0</td>
<td data-title="ECTS">0</td>
</tr>
<tr>
<td data-title="Short">MATM</td>
<td data-title="Exam">Lorem (1/2)</td>
<td data-title="P">4</td>
<td data-title="S">4</td>
<td data-title="A">0</td>
<td data-title="SJ">0</td>
<td data-title="TJ">0</td>
<td data-title="PK">0</td>
<td data-title="ECTS">0</td>
</tr>
<tr>
<td data-title="Short">MATM</td>
<td data-title="Exam">Lorem (1/2)</td>
<td data-title="P">4</td>
<td data-title="S">4</td>
<td data-title="A">0</td>
<td data-title="SJ">0</td>
<td data-title="TJ">0</td>
<td data-title="PK">0</td>
<td data-title="ECTS">0</td>
</tr>
<tr>
<td data-title="Short">MATM</td>
<td data-title="Exam">Lorem</td>
<td data-title="P">4</td>
<td data-title="S">4</td>
<td data-title="A">0</td>
<td data-title="SJ">0</td>
<td data-title="TJ">0</td>
<td data-title="PK">0</td>
<td data-title="ECTS">0</td>
</tr>
<tr>
<td data-title="Short">MATM</td>
<td data-title="Exam">Lorem (1/2)
</td>
<td data-title="P">4</td>
<td data-title="S">4</td>
<td data-title="A">0</td>
<td data-title="SJ">0</td>
<td data-title="TJ">0</td>
<td data-title="PK">0</td>
<td data-title="ECTS">0</td>
</tr>
<tr>
<td data-title="Short">MATM</td>
<td data-title="Exam">Lorem (1/2)</td>
<td data-title="P">4</td>
<td data-title="S">4</td>
<td data-title="A">0</td>
<td data-title="SJ">0</td>
<td data-title="TJ">0</td>
<td data-title="PK">0</td>
<td data-title="ECTS">0</td>
</tr>
<tr>
<td data-title="Short">MATM</td>
<td data-title="Exam">Lorem (1/2)</td>
<td data-title="P">4</td>
<td data-title="S">4</td>
<td data-title="A">0</td>
<td data-title="SJ">0</td>
<td data-title="TJ">0</td>
<td data-title="PK">0</td>
<td data-title="ECTS">0</td>
</tr>
<tr>
<td data-title="Short">-</td>
<td data-title="Exam">Lorem: 1</td>
<td data-title="P">-</td>
<td data-title="S">-</td>
<td data-title="A">-</td>
<td data-title="SJ">-</td>
<td data-title="TJ">-</td>
<td data-title="PK">-</td>
<td data-title="ECTS">-
</td>
</tr>
<tr>
<td data-title="Short">-</td>
<td data-title="Exam">Lorem: 1</td>
<td data-title="P">-</td>
<td data-title="S">-</td>
<td data-title="A">-</td>
<td data-title="SJ">-</td>
<td data-title="TJ">-</td>
<td data-title="PK">-</td>
<td data-title="ECTS">-
</td>
</tr>
</tbody>
</table>
</div>
<div class="table-responsive">
<table class="responsive-table table-bordered ">
<thead>
<tr>
<th scope="col">Year</th>
<th scope="col">Short</th>
<th scope="col">Exam</th>
<th scope="col">P</th>
<th scope="col">S</th>
<th scope="col">A</th>
<th scope="col">SJ</th>
<th scope="col">TJ</th>
<th scope="col">PK</th>
<th scope="col">ECTS</th>
</tr>
</thead>
<tbody>
<tr>
<th class="sem" scope="row">1</th>
<td data-title="Short">Lor</td>
<td data-title="Exam">Lorem</td>
<td data-title="P">4</td>
<td data-title="S">4</td>
<td data-title="A">0</td>
<td data-title="SJ">0</td>
<td data-title="TJ">0</td>
<td data-title="PK">0</td>
<td data-title="ECTS">0</td>
</tr>
<tr>
<td data-title="Short">MATM</td>
<td data-title="Exam">Lorem (1/2)</td>
<td data-title="P">4</td>
<td data-title="S">4</td>
<td data-title="A">0</td>
<td data-title="SJ">0</td>
<td data-title="TJ">0</td>
<td data-title="PK">0</td>
<td data-title="ECTS">0</td>
</tr>
<tr>
<td data-title="Short">MATM</td>
<td data-title="Exam">Lorem (1/2)</td>
<td data-title="P">4</td>
<td data-title="S">4</td>
<td data-title="A">0</td>
<td data-title="SJ">0</td>
<td data-title="TJ">0</td>
<td data-title="PK">0</td>
<td data-title="ECTS">0</td>
</tr>
<tr>
<td data-title="Short">MATM</td>
<td data-title="Exam">Lorem</td>
<td data-title="P">4</td>
<td data-title="S">4</td>
<td data-title="A">0</td>
<td data-title="SJ">0</td>
<td data-title="TJ">0</td>
<td data-title="PK">0</td>
<td data-title="ECTS">0</td>
</tr>
<tr>
<td data-title="Short">MATM</td>
<td data-title="Exam">Lorem (1/2)
</td>
<td data-title="P">4</td>
<td data-title="S">4</td>
<td data-title="A">0</td>
<td data-title="SJ">0</td>
<td data-title="TJ">0</td>
<td data-title="PK">0</td>
<td data-title="ECTS">0</td>
</tr>
<tr>
<td data-title="Short">MATM</td>
<td data-title="Exam">Lorem (1/2)</td>
<td data-title="P">4</td>
<td data-title="S">4</td>
<td data-title="A">0</td>
<td data-title="SJ">0</td>
<td data-title="TJ">0</td>
<td data-title="PK">0</td>
<td data-title="ECTS">0</td>
</tr>
<tr>
<td data-title="Short">MATM</td>
<td data-title="Exam">Lorem (1/2)</td>
<td data-title="P">4</td>
<td data-title="S">4</td>
<td data-title="A">0</td>
<td data-title="SJ">0</td>
<td data-title="TJ">0</td>
<td data-title="PK">0</td>
<td data-title="ECTS">0</td>
</tr>
<tr>
<td data-title="Short">-</td>
<td data-title="Exam">Lorem: 1</td>
<td data-title="P">-</td>
<td data-title="S">-</td>
<td data-title="A">-</td>
<td data-title="SJ">-</td>
<td data-title="TJ">-</td>
<td data-title="PK">-</td>
<td data-title="ECTS">-
</td>
</tr>
<tr>
<td data-title="Short">-</td>
<td data-title="Exam">Lorem: 1</td>
<td data-title="P">-</td>
<td data-title="S">-</td>
<td data-title="A">-</td>
<td data-title="SJ">-</td>
<td data-title="TJ">-</td>
<td data-title="PK">-</td>
<td data-title="ECTS">-
</td>
</tr>
</tbody>
</table>
</div>
TA贡献1788条经验 获得超4个赞
您有两个tbody
元素,但var tbody = document.querySelector('tbody');
只获取第一个。您需要将它们全部获取并循环遍历它们。
- 2 回答
- 0 关注
- 88 浏览
添加回答
举报