我现在正在实现类似 this post 的东西(具体来说,效果在单击该行时发生)
我如何知道表中行的索引是什么?
最佳答案
如果您直接在 tr
元素上定义点击处理程序,您可以像这样使用 index
方法:
$('#tableId tr').click(function () {
var rowIndex = $('#tableId tr').index(this); //index relative to the #tableId rows
});
如果点击事件没有直接绑定(bind)在 tr
元素上(如果你使用的是 anchor 、按钮等...),你应该找到最接近的 tr
获取正确的索引:
$(selector).click(function () {
var rowIndex = $('#tableId tr').index($(this).closest('tr'));
return false;
});
试一个例子here .
关于jquery - 如何使用 jQuery 知道 <table> 中 <tr> 的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1858175/