如何使用jQuery在表中查找行的索引 [英] How to find the index of a row in a table using jquery

查看:71
本文介绍了如何使用jQuery在表中查找行的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在表中查找行的索引.我正在尝试使用以下代码,但似乎得到的索引为-1.

I'm trying to find the index of a row in a table. I'm trying to use the following code, but I seem to get an index of -1.

$(document).ready(function()
{
    $("tr").click(function (){
        var index = $("table").index($(this));
        $("span").text("That was row index #" + index);
    });
});

使用类似这样的html;

With html that looks like this;

<table>
<tbody>
    <tr><td>click</td></tr>
    <tr><td>click</td></tr>
    <tr><td>click</td></tr>
    <tr><td>click</td></tr>
</tbody>

谢谢

推荐答案

您是否尝试过:

$("tr").index(this)

文档显示仅通过此操作,并且前面的选择应该在找到节点的位置.如果需要在特定的表中找到它(并且有多个表),则可能需要提供一些上下文:

The documentation shows just passing this and that the preceding selection should be where the node is found. If you need to find it in a specific table (and there are multiple), you may need to provide some context:

// haven't tested this
$("tr", $(this).closest("table")).index(this) 

这篇关于如何使用jQuery在表中查找行的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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