admin管理员组

文章数量:1183729

So I have this table, and when I click on a td I would like to know where is that(which row and cell) without any attributes on the elements.

<table>
    <tbody>
        <tr>
            <td>1</td>
            <td>2</td> // If I click on this I would like to know tr:1 & td:2
            <td>3</td>
        </tr>

        <tr>
            <td>4</td>
            <td>5</td>
            <td>6</td>
        </tr>

        <tr>
            <td>7</td>
            <td>8</td>
            <td>9</td>
        </tr>
    </tbody>
</table>

Javascript:

// Track onclicks on all td elements

var table = document.getElementsByTagName("table")[0];
var cells = table.getElementsByTagName("td"); // 

for(var i = 1; i < cells.length; i++){
    // Cell Object
    var cell = cells[i];
    // Track with onclick
    cell.onclick = function(){
        // Track my location;
        // example: I'm in table row 1 and I'm the 2th cell of this row
    }
}

So I have this table, and when I click on a td I would like to know where is that(which row and cell) without any attributes on the elements.

<table>
    <tbody>
        <tr>
            <td>1</td>
            <td>2</td> // If I click on this I would like to know tr:1 & td:2
            <td>3</td>
        </tr>

        <tr>
            <td>4</td>
            <td>5</td>
            <td>6</td>
        </tr>

        <tr>
            <td>7</td>
            <td>8</td>
            <td>9</td>
        </tr>
    </tbody>
</table>

Javascript:

// Track onclicks on all td elements

var table = document.getElementsByTagName("table")[0];
var cells = table.getElementsByTagName("td"); // 

for(var i = 1; i < cells.length; i++){
    // Cell Object
    var cell = cells[i];
    // Track with onclick
    cell.onclick = function(){
        // Track my location;
        // example: I'm in table row 1 and I'm the 2th cell of this row
    }
}
Share Improve this question edited Dec 21, 2016 at 14:53 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Feb 15, 2011 at 0:52 Adam HalaszAdam Halasz 58.3k67 gold badges153 silver badges216 bronze badges 4
  • jQuery has the helpful $.index() method that can do this for you if you're open to frameworks. – Sampson Commented Feb 15, 2011 at 0:54
  • I don't wanna use any frameworks, but I will try to look what does that function do in jQuery. – Adam Halasz Commented Feb 15, 2011 at 0:55
  • You can see an example using $.index() online at http://jsbin.com/okuri4 but @patrickdw's answer below seems to be better for you. – Sampson Commented Feb 15, 2011 at 1:08
  • this might help: stackoverflow.com/questions/661726/how-to-get-a-cells-location – Gordon Gustafson Commented Feb 15, 2011 at 1:38
Add a comment  | 

4 Answers 4

Reset to default 24

In the handler, this is the table cell, so for the cell index do this:

var cellIndex  = this.cellIndex + 1;  // the + 1 is to give a 1 based index

and for the row index, do this:

var rowIndex = this.parentNode.rowIndex + 1;

Example: http://jsfiddle.net/fwZTc/1/

This script block will provide you the information you desire, by adding the information as properties to the cell and then accessing them in the onclick function:

// Track onclicks on all td elements
var table = document.getElementsByTagName("table")[0];
// Get all the rows in the table
var rows = table.getElementsByTagName("tr");

for (var i = 0; i < rows.length; i++) {
    //Get the cells in the given row
    var cells = rows[i].getElementsByTagName("td");
    for (var j = 0; j < cells.length; j++) {
        // Cell Object
        var cell = cells[j];
        cell.rowIndex = i;
        cell.positionIndex = j;
        cell.totalCells = cells.length;
        cell.totalRows = rows.length;
        // Track with onclick
        console.log(cell);
        cell.onclick = function () {
            alert("I am in row " + this.rowIndex + " (out of " + this.totalRows + " rows) and I am position " + this.positionIndex + " (out of " + this.totalCells + " cells)");
        };
    }
}

Well, When you have rowspan/colspan you can have a lot more fun, however, if the grid is regular, you can just determine your position from the index by doing:

row = Math.floor(i / rows); 
column = i % columns;

Using "this" on cells table

function myFunction(x) {
        var tr = x.parentNode.rowIndex;
        var td = x.cellIndex;        
      
        document.getElementById("tr").innerHTML = "Row index is: " + tr;
        document.getElementById("td").innerHTML = "Column index is: " + td;
      }
tr, th, td {
  padding: 0.6rem;
  border: 1px solid black
}

table:hover {
  cursor: pointer;
}
<table>
    <tbody>
        <tr>
            <td onclick="myFunction(this)">1</td>
            <td onclick="myFunction(this)">2</td>
            <td onclick="myFunction(this)">3</td>
        </tr>

        <tr>
            <td onclick="myFunction(this)">4</td>
            <td onclick="myFunction(this)">5</td>
            <td onclick="myFunction(this)">6</td>
        </tr>

        <tr>
            <td onclick="myFunction(this)">7</td>
            <td onclick="myFunction(this)">8</td>
            <td onclick="myFunction(this)">9</td>
        </tr>
    </tbody>
</table>

<p id="tr"></p>
<p id="td"></p>

本文标签: javascriptGet Cell LocationStack Overflow