[英]jQuery syntax for finding row index in a HTML table
1) How do I find the row number/index in a HTML table? 1)如何在HTML表中找到行号/索引? The generated table doesn't have any id for row.
生成的表没有任何行ID。
eg: I have a plain HTML table generated, which has 10 rows, I am adding rows dynamically to this table.(in between existing rows) 例如:我生成了一个纯HTML表,有10行,我在这个表中动态添加行。(在现有行之间)
Since I am adding new row, the existing row index will change. 由于我要添加新行,现有的行索引将会更改。 Now I need to to find the index of each row before adding the new row.
现在我需要在添加新行之前找到每行的索引。
"1) How do i find the row number/index in a HTML table? The generated table dosen't have any id for row."
“1)如何在HTML表中找到行号/索引?生成的表没有任何行的id。”
If you mean that you already have a row, and you need its index, don't use jQuery to get it. 如果你的意思是你已经有了一行,并且你需要它的索引,那就不要使用jQuery来获取它。 Table rows maintain their own index via the
rowIndex
property. 表行通过
rowIndex
属性维护自己的索引。
$('table tr').click(function() {
alert( this.rowIndex ); // alert the index number of the clicked row.
});
Demo: http://jsfiddle.net/LsSXy/ 演示: http : //jsfiddle.net/LsSXy/
To get the index of any element within a selector use index()
. 要获取选择器中任何元素的索引,请使用
index()
。
In your case it would be: 在你的情况下,它将是:
var rowIndex = $("#myTable TR").index();
In addition, you can use eq()
to select a specific element in a group: 此外,您可以使用
eq()
选择组中的特定元素:
var thirdRow = $("#myTable TR").eq(2) // zero based .: 2 = 3rd element.
The jQuery site is really good for finding functions, I always find myself going back to it all the time for reference and refresh. jQuery网站非常适合查找函数,我总是发现自己会一直回到它以供参考和刷新。 http://docs.jquery.com/
http://docs.jquery.com/
Or you can use css selectors in jquery like so 或者你可以在jquery中使用css选择器
$('table tr td:first').addClass('first-row');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.