我有一个需要过滤的HTML表。我不知道为什么它不起作用。 我试图将i放入“”,但没有成功。我认为错误是与for循环有关,或者与querySelector有关。
function filtern(){
var reih = document.querySelector('.mon_list').rows.length
for (var i= 2: i <= reih; i++;){
if ((document.querySelector('.mon_list tr:nth-Child(i) td:nth-Child(1)').innerHTML) != ("5a")){
document.querySelector('.mon_list tr:nth-Child(i)').style.display='none';
}
}
document.getElementById("demo2").innerHTML = document.querySelector('.mon_list tr:nth-Child(2) td:nth-Child(1)').innerHTML;
document.getElementById('demo').innerHTML = reih;
}
filtern();
和HTML
<head>
</head>
<body>
<p>
<table class="mon_list" >
<tr class='list'><th class="list" align="center"><b>Klasse(n)</b></th>
<th class="list" align="center">Stunde</th>
<th class="list" align="center">(Lehrer)</th>
<th class="list" align="center"><b>Vertreter</b>
</th><th class="list" align="center">Fach</th>
<th class="list" align="center">Raum</th><th class="list" align="center">Vertretungs-Text</th>
</tr>
<tr class='list odd'><td class="list" align="center">
<b>5a</b></td><td class="list" align="center">5</td>
<td class="list" align="center">Se</td>
<td class="list" align="center"><b>Ma</b>
</td><td class="list" align="center">BNT-b</td>
<td class="list" align="center">2.25</td>
<td class="list" align="center">Vertretung</td>
</tr>
<tr class='list even'><td class="list" align="center"><b>5a</b></td>
<td class="list" align="center">6</td>
<td class="list" align="center">Se</td>
<td class="list" align="center"><b>---</b></td>
<td class="list" align="center">---</td>
<td class="list" align="center">---</td>
<td class="list" align="center">frei</td>
</tr>
</table>
</p>
<p id="demo"></p>
<p id="demo2"></p>
</body>
答案 0 :(得分:1)
更改if条件和内部语句会注意到变量' + i + '
的串联。您已将其用作字符串,它应该是数字。
if ((document.querySelector('.mon_list tr:nth-Child(' + i + ') td:nth-Child(1)').innerHTML) != ("5a")){
document.querySelector('.mon_list tr:nth-Child(' + i + ')').style.display='none';
}
也在for循环中使用分号(;)代替冒号(:),并在i++
之后删除分号
for (var i = 2; i <= reih; i++) {