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

后端 未结 4 1263
臣服心动
臣服心动 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: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/

提交回复
热议问题