2009-03-02 11 views
15

子要素の位置を見つける必要があります。子要素の位置を取得する方法

私はテーブルを持っているとTDをクリックしたときに、私は(0,1または2)この

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

トリックGPS追跡システムを取り付けるに親要素。 –

+1

@AdamDavisこのソリューションは、子要素が青春になるまでのみ機能します。 – Neil

答えて

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

<table> 
<tr> 
<td> 

</td> 
<td> 

</td> 
<td> 

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

とスクリプトTDの位置をしたいです

編集:私はそれを試して、それが動作します

0

参考のため、ちょうど参考にこれは良いです

<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> 

refer here

関連する問題