单击HTML表并获取行号(使用Javascript,而不是jQuery)

单击HTML表并获取行号(使用Javascript,而不是jQuery),javascript,html,html-table,row,Javascript,Html,Html Table,Row,我想知道如何单击HTML表中的按钮并将行数和列数返回给我:例如,使用下表: <table> <tr> <td><input type="button" value="button"></td> <td><input type="button" value="button"></td> <td><input type="button" value="butt

我想知道如何单击HTML表中的按钮并将行数和列数返回给我:例如,使用下表:

<table>
  <tr>
    <td><input type="button" value="button"></td>
    <td><input type="button" value="button"></td>
    <td><input type="button" value="button"></td>
    </tr>
    <tr>
    <td><input type="button" value="button"></td>
    <td><input type="button" value="button"></td>
    <td><input type="button" value="button"></td>
    </tr>
    <tr>
    <td><input type="button" value="button"></td>
    <td><input type="button" value="button"></td>
    <td><input type="button" value="button"></td>
    </tr>
  </table>

如何使用JavaScript单击第二行的第一个按钮,并让它告诉我我单击了第二行的第一个单元格?每个按钮是否需要有唯一的id?

试试以下方法:

函数getId(元素){
警报(“行”+element.parentNode.parentNode.rowIndex+
“-column”+元素.parentNode.cellIndex);
}

最通用的@Gremash js函数版本

function  getId(element) {
    alert("row" + element.closest('tr').rowIndex + 
    " -column" + element.closest('td').cellIndex);
}

争吵不断。细胞也有。您可以使用它们您可以轻松地将其添加到TD并删除其中一个parentNode函数以不使用按钮。我尝试了“element.parentNode.parentNode.rowIndex”,但它返回undefinedSuperman。。。。。。我通过jQuery获得了很多解决方案。我想要Javascript。知道了。。。。。