如何知道< tr>的索引在< table>内部与jQuery? [英] How to know index of a <tr> inside a <table> with jQuery?

查看:88
本文介绍了如何知道< tr>的索引在< table>内部与jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在实施类似于这篇文章(具体来说,这个效果发生在点击该行时)



我如何知道表格中的行索引是什么?

解决方案

如果您直接在 tr 元素上定义点击处理程序,你可以像这样使用 index 方法:

  $(' #tableId tr')。click(function(){
var rowIndex = $('#tableId tr')。index(this); //相对于#tableId行的索引
});

如果click事件不是直接绑定在 tr 元素(如果您使用的是锚点,按钮等),您应该找到最接近的 tr 来获得正确的索引:


$ b $ $ $ $ $ $ $ $ $ $($)$($)选择器.click(函数(){
var rowIndex = $('#tableId tr')。index($ (this).closest('tr'));

return false;
});

试试一个例子这里


I'm now implementing something like this post (specifically, the effect takes place when the row is clicked)

How do I know what the index of the row is inside the table?

解决方案

If you defined the click handler directly on the tr elements, you can use the index method like this:

$('#tableId tr').click(function () {
  var rowIndex = $('#tableId tr').index(this); //index relative to the #tableId rows
});

If the click event is bound not directly on the tr element (if you are using an anchor, a button, etc...), you should find the closest tr to get the right index:

$(selector).click(function () {
  var rowIndex = $('#tableId tr').index($(this).closest('tr'));

  return false;
});

Try an example here.

这篇关于如何知道< tr>的索引在< table>内部与jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆