简体   繁体   English

根据第一个单元格的值在JavaScript中隐藏表格行

[英]hide table rows in JavaScript based on value of first cell

I have an HTML table i need to filter. 我有一个需要过滤的HTML表。 I don't know why it won't work. 我不知道为什么它不起作用。 I've tried to put the i in '' but it did't worked. 我试图将i放入“”,但没有成功。 I think the error is with the for loop, or in the querySelector. 我认为错误与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();

and the HTML 和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>

Change your if condition and inner statements notice the concatenation of variable ' + i + ' . 更改您的if条件和内部语句会注意到变量' + i + '的串联。 You have used it as string it should be number. 您已将其用作字符串,它应该是数字。

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

also in for loop use semicolon(;) instead of colon(:) and remove semicolon after i++ 同样在for循环中使用分号(;)代替冒号(:)并在i++之后删除分号

for (var i = 2; i <= reih; i++) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM