Selecting an arbitrary cell in a table by row and column number

后端 未结 4 1244
臣服心动
臣服心动 2020-12-31 07:26

I have a large table, and I need to be able to select a specific cell using it\'s cell/row coordinates.

What\'s the most elegant way of doing this using jQuery?

相关标签:
4条回答
  • 2020-12-31 07:48

    I'm pretty sure this selects the cell at coordinate (9, 9). Let me test:

    $('table tr:eq(10) > td:eq(10)')
    
    0 讨论(0)
  • 2020-12-31 07:48

    $($("table#wall_layout tr")[row]).find("td")[col]

    0 讨论(0)
  • 2020-12-31 07:58

    This is one case where I think using native JavaScript actually makes the code easier to understand:

    var table = $("#table")[0];
    var cell = table.rows[1].cells[1]; // This is a DOM "TD" element
    var $cell = $(cell); // Now it's a jQuery object.
    

    Note that just selecting the table element will make rows include those rows in your thead (and tfoot). What you probably want is:

    var table = $("#table tbody")[0];
    /* remaining code from above */
    

    Here's an example: http://jsfiddle.net/CgqQt/

    0 讨论(0)
  • 2020-12-31 08:06

    After reviewing the fiddle you posted in one of your comments, this could also work.

    http://jsfiddle.net/CGrP9/6/

    $('tbody tr').eq(2).find('td').eq(2).css('background-color', 'green');
    
    0 讨论(0)
提交回复
热议问题