Как получить позицию дочернего элемента

Мне нужно найти положение дочернего элемента.

У меня есть таблица, и когда нажата кнопка td, я хочу, чтобы позиция td (0,1 или 2)

<table>
<tr>
 <td>   

 </td>
 <td>   

 </td>
 <td>

 </td>
</tr>
</table>

и a script, как этот

<script>
$("td").click(function(){
  //how do i get the position of the td?
  alert("column " + columnPosition + "is clicked")
});
</script>

Ответы

Ответ 1

<script>
$("td").click(function(){
  //how do i get the position of the td?
  alert("column " + $(this).parent().children().index(this) + " is clicked")
});
</script>

edit: Я тестировал его, и он работает

Ответ 2

Просто для справки, и это хорошо

<div>First div</div>
<div>Second div</div>
<div>Third div</div>
<div>Fourth div</div>

<script>
 $( "div" ).click(function() {
    // `this` is the DOM element that was clicked
    var index = $( "div" ).index( this );
    $( "span" ).text( "That was div index #" + index );
 });
</script>

см. здесь