javascript 如何使用行索引和 td 索引获取 td 中的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13137597/
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-10-26 17:54:26  来源:igfitidea点击:

How to get element inside a td using Row index and td index

javascriptjquery

提问by Arian

I have a Row Indexand TD indexin a table and I want to select inputelement inside the cell in [Row Index,TD Index]. How I can do this?

我在表格中有一个Row Indexand TD index,我想input[Row Index,TD Index]. 我怎么能做到这一点?

采纳答案by Viktor S.

This should work:

这应该有效:

$('tr:eq(rowIndex) td:eq(tdIndex) input')

:eqselector for more information.

:eq选择器以获取更多信息。

回答by Alnitak

Tables have accessor properties intended for direct access to individual cells, i.e.:

表具有用于直接访问单个单元格的访问器属性,即:

table.rows[rowIndex].cells[colIndex]

hence:

因此:

table.rows[rowIndex].cells[colIndex].getElementsByTagName('input')[0];

or:

或者:

$('input', table.rows[rowIndex].cells[colIndex])

回答by galchen

var rowIndex = X;
var cellIndex = Y;
$('#my-table tbody')
    .children(':nth-child('+(rowIndex+1)+')')
      .children(':nth-child('+(cellIndex+1)+')')
        .find('input').val('Hello');

of course you can put em all a single selector

当然你可以把它们都放在一个选择器中

$('#my-table tbody tr:nth-child('+(rowIndex+1)+') td:nth-child('+(cellIndex+1)+')')
        .find('input').val('Hello');