首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >检查表格行中的单元格是否等于0,如果为0则隐藏行。

检查表格行中的单元格是否等于0,如果为0则隐藏行。
EN

Stack Overflow用户
提问于 2015-02-12 16:53:44
回答 1查看 2.8K关注 0票数 0

本质上,我想要做的是遍历一个表行,并检查除第一列以外的所有列是否都等于零。如果是,那么隐藏行。

代码语言:javascript
代码运行次数:0
运行
复制
<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/

代码语言:javascript
代码运行次数:0
运行
复制
$('#table tr td').each(function() {
    if ($(this).text() === '0'){
        $(this).css('color', 'red');
    }
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-12 17:03:04

您需要找到行,计算每行中的单元格,如果该行符合您的条件,则应用隐藏类:重新标记的JSFiddle演示

代码语言:javascript
代码运行次数:0
运行
复制
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'
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28483222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档