How to get cells () x and y coordinate in table using jQuery?

后端 未结 2 479
日久生厌
日久生厌 2020-12-28 18:10

I\'m looking for a good way to get cells X-Y position in a table. (do not confuse it with css-position, I am looking for X and Y coordinate in Cartesian coordinate system).<

相关标签:
2条回答
  • window.onload = function () {
      document.getElementsByTagName('table')[0].addEventListener('click', function(e) {
        alert("My position in table is: " + e.target.parentElement.rowIndex + "x " + e.target.cellIndex + "y");
      }, false);
    }
    tr, td {
      padding: 0.6rem;
      border: 1px solid black
    }
    
    table:hover {
      cursor: pointer;
    }
    <table id="grid">
      <tr>
        <td>0, 0</td>
        <td>0, 1</td>
        <td>0, 2</td>
      </tr>
      <tr>
        <td>1, 0</td>
        <td>1, 1</td>
        <td>1, 2</td>
      </tr>
    </table>

    0 讨论(0)
  • 2020-12-28 18:48

    DOM Level 2 HTML cellIndex:

    alert('My position in table is: '+this.cellIndex+'x'+this.parentNode.rowIndex);
    
    0 讨论(0)
提交回复
热议问题