如何使用jQuery获取单击的表格单元格的行号和列号,即
$("td").onClick(function(event){
var row = ...
var col = ...
});
Run Code Online (Sandbox Code Playgroud)
CMS*_*CMS 211
您可以在给定的上下文中使用Core/index函数,例如,您可以在其父TR中检查TD的索引以获取列号,并且可以检查表上的TR索引,以获取行号:
$('td').click(function(){
var col = $(this).parent().children().index($(this));
var row = $(this).parent().parent().children().index($(this).parent());
alert('Row: ' + row + ', Column: ' + col);
});
Run Code Online (Sandbox Code Playgroud)
在这里查看一个运行示例.
Phi*_*enn 115
$('td').click(function() {
var myCol = $(this).index();
var $tr = $(this).closest('tr');
var myRow = $tr.index();
});
Run Code Online (Sandbox Code Playgroud)
小智 29
点击获取COLUMN INDEX:
$(this).closest("td").index();
Run Code Online (Sandbox Code Playgroud)
点击获取ROW INDEX:
$(this).closest("tr").index();
Run Code Online (Sandbox Code Playgroud)
Sam*_*ham 21
在我的头顶,一种方法是抓住所有以前的元素并计算它们.
$('td').click(function(){
var colIndex = $(this).prevAll().length;
var rowIndex = $(this).parent('tr').prevAll().length;
});
Run Code Online (Sandbox Code Playgroud)
在创建表格时,您可以在单元格中输出该数据吗?
所以你的表看起来像这样:
<table>
<thead>...</thead>
<tbody>
<tr><td data-row='1' data-column='1'>value</td>
<td data-row='1' data-column='2'>value</td>
<td data-row='1' data-column='3'>value</td></tr>
<tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
那么这将是一件简单的事情
$("td").click(function(event) {
var row = $(this).attr("data-row");
var col = $(this).attr("data-col");
}
Run Code Online (Sandbox Code Playgroud)