jQuery中的表行和列号 [英] Table row and column number in jQuery

查看:75
本文介绍了jQuery中的表行和列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jQuery获取被点击的表格单元格的行号和列号,即

How do I get the row and column number of the clicked table cell using jQuery, i.e.,

$("td").onClick(function(event){
   var row = ...
   var col = ...
});

推荐答案

您可以使用 Core/index 在给定上下文中的功能,例如,您可以检查TD在其父TR中的索引以获取列号,并且可以在Table上检查TR索引以获取行号:

You can use the Core/index function in a given context, for example you can check the index of the TD in it's parent TR to get the column number, and you can check the TR index on the Table, to get the row number:

$('td').click(function(){
  var col = $(this).parent().children().index($(this));
  var row = $(this).parent().parent().children().index($(this).parent());
  alert('Row: ' + row + ', Column: ' + col);
});

此处中查看正在运行的示例.

Check a running example here.

这篇关于jQuery中的表行和列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆