我需要找到一个子元素的位置.
我有一个表,当点击一个td时,我想要td的位置(0,1或2)
<table>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
和这样的脚本
<script>
$("td").click(function(){
//how do i get the position of the td?
alert("column " + columnPosition + "is clicked")
});
</script>
Run Code Online (Sandbox Code Playgroud)
Tho*_*ock 35
<script>
$("td").click(function(){
//how do i get the position of the td?
alert("column " + $(this).parent().children().index(this) + " is clicked")
});
</script>
Run Code Online (Sandbox Code Playgroud)
编辑:我测试了它,它的工作原理