如何使用jQuery获取单击的表格单元格的行号和列号,即
$("td").onClick(function(event){
var row = ...
var col = ...
});
Run Code Online (Sandbox Code Playgroud) 我知道如何使用jQuery获取单元格的行和列索引,但我无法弄清楚相反的情况.给定行和列索引,我将如何访问此位置的td?
我如何在表格行中找到td元素的位置?我已经看到了这个代码的建议,但有没有办法让每个td标记包含一个onclick事件?
<table>
<tr>
<td onclick="myFunction(this)">Click to show cellIndex</td>
<td onclick="myFunction(this)">Click to show cellIndex</td>
<td onclick="myFunction(this)">Click to show cellIndex</td>
<td onclick="myFunction(this)">Click to show cellIndex</td>
</tr>
</table>
<script>
function myFunction(x) {
alert("Cell index is: " + x.cellIndex);
}
</script>
Run Code Online (Sandbox Code Playgroud)
我有一个图像网格,我试图看到网格中的哪些图像被点击.我试过这个:
$('td img').on('click', function(x){
console.log("Cell index is: " + x.cellIndex);
});
Run Code Online (Sandbox Code Playgroud)
但它只是记录 undefined