本质上,我想要做的是遍历一个表行,并检查除第一列以外的所有列是否都等于零。如果是,那么隐藏行。
<table id="table">
<thead>
<tr>
<th>Player</th>
<th>Move 1</th>
<th>Move 2</th>
<th>Move 3</th>
<th>Move 4</th>
<th>Move 5</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>James</td>
<td>0</td>
<td>5</td>
<td>3</td>
<td>4</td>
<td>0</td>
</tr>
</tbody>
</table>
因此,在上面的表中,第一行将被隐藏,第二行将被显示。
任何帮助都将不胜感激!
更新:示例:http://jsfiddle.net/3rnbk6s5/
$('#table tr td').each(function() {
if ($(this).text() === '0'){
$(this).css('color', 'red');
}
});
发布于 2015-02-12 09:03:04
您需要找到行,计算每行中的单元格,如果该行符合您的条件,则应用隐藏类:重新标记的JSFiddle演示。
var tbl = document.getElementById('table'); //find the table
var rows = tbl.querySelectorAll('tbody tr'); //find all rows in the table body
for(i = 0; i < rows.length; i++) { //iterate through the rows
var cells = rows[i].querySelectorAll('td'); //find all of the cells in the row
var flag = true; //set flag prior to cell evaluation
for(j = 2; j < cells.length; j++) { //iterate through the cells (starting with the cell at position 2)
if (cells[j].innerHTML != '0') { //check if the cell contains '0' (set flag to false if cell is not '0')
flag = false;
}
}
if(flag) {
rows[i].classList.add('hide'); //hide the row if the falg remained true (i.e. none of the cells contained a value other than '0'
}
}
https://stackoverflow.com/questions/28483222
复制相似问题