Mah*_*dam 2 html javascript jquery
我如何在表格行中找到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
您可以在jquery中使用$ .index()函数来获取对象的索引
function myFunction(x) {
alert($(x).index());
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.