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
How to get element inside a td using Row index and td index
提问by Arian
I have a Row Index
and TD index
in a table and I want to select input
element inside the cell in [Row Index,TD Index]
. How I can do this?
我在表格中有一个Row Index
and TD index
,我想input
在[Row Index,TD Index]
. 我怎么能做到这一点?
采纳答案by Viktor S.
回答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');