jQuery - 获取控件的 td 单元格索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9150744/
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
jQuery - get td cell index of a control
提问by tempid
** EDIT** I'm very sorry. I need the td index, not the row index.
** 编辑** 非常抱歉。我需要 td 索引,而不是行索引。
Using jQuery, how do I get the index of the td lblName
is in?
使用 jQuery,如何获取 td 的索引lblName
?
<table>
<tr>
<td>
</td>
<td>
<label id="lblName" />
</td>
<td>
</td>
</tr>
</table>
回答by
Update for updated question:
更新更新的问题:
$('#lblName').parent().index();
or
或者
document.getElementById('lblName').parentNode.cellIndex
Original answer:
原答案:
Use .index()
.
使用.index()
.
$('#lblName').closest('tr').index();
The .index()
method has a few different use patterns.
该.index()
方法有几种不同的使用模式。
This one means "take the first element matched by the selector, and return its zero based index of its position among its siblings".
这意味着“采用选择器匹配的第一个元素,并返回其在其兄弟中的位置的从零开始的索引”。
Or natively, do this...
或者本机,这样做...
document.getElementById('lblName').parentNode.parentNode.rowIndex
Or combine the two...
或者两者结合...
$('#lblName').closest('tr')[0].rowIndex;
回答by dfsq
var index = $('#lblName').closest('tr').index();
回答by Royi Namir
$("table tr").index($("#lblName").parents("tr:first"))
回答by Lokesh Yadav
Hope, this piece of code would be of any help.
希望,这段代码会有所帮助。
You can use the index() with parentsUntill() to the parent TR element of that TD.
您可以将 index() 与 parentsUntill() 一起用于该 TD 的父 TR 元素。
example http://jsfiddle.net/ylokesh/bGvHZ/
示例http://jsfiddle.net/ylokesh/bGvHZ/
$('#lblName').parentsUntil('tr').index()