admin管理员组文章数量:1429067
<table>
<tr>
<td>me</td>
<td>you</td>
<td>we</td>
</tr>
</table>
Using Jquery i want to know which childrin was clicked and get the number of this childrin. like, if I click you
, i want to get 2 and if we
, then 3.. if me
, then 1.
is there something like .nthchild();
in Jquery?
here is my fiddle to test: /
<table>
<tr>
<td>me</td>
<td>you</td>
<td>we</td>
</tr>
</table>
Using Jquery i want to know which childrin was clicked and get the number of this childrin. like, if I click you
, i want to get 2 and if we
, then 3.. if me
, then 1.
is there something like .nthchild();
in Jquery?
here is my fiddle to test: http://jsfiddle/vfp9x/
Share Improve this question edited Jul 1, 2014 at 7:01 doniyor asked Jul 1, 2014 at 6:53 doniyordoniyor 38k61 gold badges181 silver badges270 bronze badges3 Answers
Reset to default 9Simply use $(this).index()
, you also have wrong id for table
in live demo, it should be table1
Remember the index is zero based so you will get zero 0
for first element and 1 for second element and so on.
Live Demo
$('#table1').on('click', 'td', function () {
$('#out').text($(this).index()+1);
});
.index()
If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.
Try,
$('#table1').on('click','td',function(){
var child_number = $(this).closest('tr').find('td').index(this);
$('#out').text(child_number);
});
DEMO
Simply use .index()
as per the other answer provider suggested, and also keep this way of getting the index also. it would helpful for you in some other contexts, like getting the index of the current element from a collection of elements (not siblings to each other)
Try Like
$("#ul").on('click', 'td', function () {
debugger;
alert(this.id);
});
Demo
本文标签: javascriptjqueryget the position number of clicked td in a tableStack Overflow
版权声明:本文标题:javascript - jquery - get the position number of clicked td in a table - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745543391a2662596.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论