Getting td by index with jQuery

后端 未结 3 1064
感动是毒
感动是毒 2020-12-13 09:00

I know how to get a cell\'s row and column index with jQuery, but I can\'t figure out the reverse. Given a row and column index, how would I access the td at this location?<

相关标签:
3条回答
  • 2020-12-13 09:43

    With plain JavaScript:

    // table is a reference to your table
    table.rows[rowIndex].cells[columnIndex]
    

    Reference: HTMLTableElement, HTMLTableRowElement


    With jQuery, you could use .eq():

    $('#table tr').eq(rowIndex).find('td').eq(columnIndex)
    // or
    $('#table tr:eq(' + rowIndex + ') td:eq(' + columnIndex + ')')
    
    0 讨论(0)
  • 2020-12-13 09:48

    You can use the :eq selector:

    var row = 1;
    var col = 2;
    var cell = $('table tr:eq(' + row + ') td:eq(' + col + ')');
    

    Here's an example of this in action

    0 讨论(0)
  • 2020-12-13 09:58

    How about using the nth-child selector?

    http://api.jquery.com/nth-child-selector/

    var row = 4;
    var col = 2
    
    var cell = $('table#tableId tr:nth-child(' + row + ') td:nth-child(' + col + ')');
    

    Note that the child index is 1-based, rather than the more usual 0-based.

    0 讨论(0)
提交回复
热议问题