JQuery: How to select rows from a table

后端 未结 3 1315
不知归路
不知归路 2020-12-07 03:01

I have a scenario where I would like to select rows from a table depending upon the values in td

e.g. I have a table like this


   

        
3条回答
  •  执笔经年
    2020-12-07 03:43

    You can use the native DOM cells property on the HTMLTableRowElement to access the cells directly, rather than sending jQuery on a roundabout trip with listing all descendant cells and picking out one with the non-standard :eq selector.

    $('tr').each(function (index) {
        if (
            $(this.cells[0]).text().trim()===x &&
            $(this.cells[1]).text().trim()===y
        ) {
            ...
        }
    });
    

    though it probably won't make a serious performance difference. Another approach would be simply to maintain an array-of-arrays containing the pre-trimmed data to save even looking at the DOM.

提交回复
热议问题