jquery - 获取表中单击的td的位置编号

don*_*yor -3 html javascript jquery

<table>
   <tr>
      <td>me</td>
      <td>you</td>
      <td>we</td>
   </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

使用Jquery我想知道点击了哪个childrin并获得了这个childrin的数量.比如,如果我点击you,我想得到2和if we,然后3 .. if me,然后1.

.nthchild();在Jquery 有类似的东西吗?

这是我的小提琴测试:http://jsfiddle.net/vfp9x/

Adi*_*dil 9

只需使用$(this).index(),你也有错的IDtable在现场演示,它应该是table1

请记住索引是基于零的,因此0第一个元素为零,第二个元素为1,依此类推.

现场演示

$('#table1').on('click', 'td', function () {    
    $('#out').text($(this).index()+1);
});
Run Code Online (Sandbox Code Playgroud)

.指数()

如果没有将参数传递给.index()方法,则返回值是一个整数,指示jQuery对象中第一个元素相对于其兄弟元素的位置.