如何找出使用jQuery单击哪个表行?

Bhu*_*han 1 jquery

我正在使用'live'函数在表行上执行一些点击操作,即

$("tr").live('click',function() {
      alert('Some table row is clicked');
});
Run Code Online (Sandbox Code Playgroud)

我想知道点击和使用哪一行if-else,根据它提供一些自定义警报.谁能告诉我怎么做?

非常感谢.

编辑1:

有没有办法可以引用函数内部单击行的元素?

gdo*_*ica 6

$("tr").live('click', function() {
    if (this.id == "foo") {
        alert('the tr with the foo id was clicked');
    }
});?
Run Code Online (Sandbox Code Playgroud)

如果要检查哪个行号,请使用index:

$("tr").live('click', function() {
   if $(this).index() === 2) {
     alert('The third row was clicked'); // Yes the third as it's zero base index
   }
});?
Run Code Online (Sandbox Code Playgroud)

现场演示


更新:

$("tr").live('click', function() {
    // "this" is the clicked <tr> element
    // $(this).find('td span') is the spans inside a td inside the clicked <tr>
}
Run Code Online (Sandbox Code Playgroud)