如何获得< td>在使用JQuery的表中的位置? [英] How to get a <td> 's position in a table using JQuery?
本文介绍了如何获得< td>在使用JQuery的表中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
<table>
<tr><td>1,1</td><td>2,1</td></tr>
<tr><td>2,1</td><td>2,2</td></tr>
</table>
我想使用以下功能:
$("td").click(function(){
alert(xxxx)
})
点击时获得< td>
的位置,但是如何?
to get the <td>
`s position when clicked, but how?
推荐答案
没有参数调用的 index
函数将获得相对于其兄弟姐妹的位置(无需遍历层次结构。)
The index
function called with no parameters will get the position relative to its siblings (no need to traverse the hierarchy).
$('td').click(function(){
var $this = $(this);
var col = $this.index();
var row = $this.closest('tr').index();
alert( [col,row].join(',') );
});
这篇关于如何获得< td>在使用JQuery的表中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文