jQuery 使用jQuery查找知道行和列ID的表格单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10277934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:04:54  来源:igfitidea点击:

Find table cell knowing row and column ids with jQuery

jqueryhtml-table

提问by eagerMoose

I have a simple table, where I have set IDs for headers and IDs for rows. Given an ID for both, I need to find a corresponding cell at the intersection of those two.

我有一个简单的表,我在其中设置了标题的 ID 和行的 ID。给定两者的 ID,我需要在这两者的交集处找到相应的单元格。

Example:

例子:

<table id="tablica">
    <thead>
        <th id="bla">Bla</th>
        <th id="bli">Bli</th>
        <th id="blu">Blu</th>
    </thead>
    <tbody>
        <tr id="jedan">
            <td>1</td>            
            <td>2</td>
            <td>3</td>
        </tr>
        <tr id="dva">
            <td>4</td>            
            <td>5</td>
            <td>6</td>
        </tr>
        <tr id="tri">
            <td>7</td>            
            <td>8</td>
            <td>9</td>
        </tr>
    </tbody>
</table>???

So if I have id="bli" and id="dva", that means I want to do something with cell having value 5 in this example.

所以如果我有 id="bli" 和 id="dva",这意味着我想在这个例子中对值为 5 的单元格做一些事情。

edit:There are a lot of correct answers, I upvoted all of them, but unfortunately I can only choose one as correct.

编辑:有很多正确的答案,我都赞成,但不幸的是我只能选择一个正确的。

采纳答案by WolvDev

Here is my solution:

这是我的解决方案:

var column = $('#bli').index();
var cell = $('#dva').find('td').eq(column);

And working example on jsfiddle: http://jsfiddle.net/t8nWf/2/

和 jsfiddle 的工作示例:http: //jsfiddle.net/t8nWf/2/

added all in a function:

在一个函数中添加了所有内容:

function getCell(column, row) {
    var column = $('#' + column).index();
    var row = $('#' + row)
    return row.find('td').eq(column);
}

Working example: http://jsfiddle.net/t8nWf/5/

工作示例:http: //jsfiddle.net/t8nWf/5/

回答by Murtaza

If you have got the id of the row you can select the column having data 5 into it.

如果您获得了行的 id,您可以选择包含数据 5 的列。

$("#dva").find("td:contains(5)").css({"background-color":"red"});

Also refer this fiddleas working example.

也将这个小提琴作为工作示例。

EDITWithout knowing the id of the row you only have id to table, then you can also find the cell :

编辑在不知道行的 id 的情况下,您只有表的 id,然后您还可以找到单元格:

$("#tablica tr").find("td:contains(5)").css({"background-color":"red", "padding":"5px"});

Working example

工作示例

回答by binarious

$('#dva > td').eq($('#bli').index()); // returns the td element

should work. Working example: http://jsbin.com/acevon/edit#javascript,html,live

应该管用。工作示例:http: //jsbin.com/acevon/edit#javascript,html,live

回答by Jan Han?i?

This will get you the TD element you want as a jQuery object:

这将使您获得想要作为 jQuery 对象的 TD 元素:

var thIndex = $( '#tablica #bli' ).index (); // get index of TH in thead
var $td = $( $( '#dva td' ).get ( thIndex ) ); // get the TD in the target TR on the same index as is in the thead

JSFiddle example

JSFiddle 示例