获取小区位置

时间:2011-02-15 00:52:15

标签: javascript dom html-table

所以我有这个表,当我点击一个td时,我想知道那个(哪个行和单元格)在元素上没有任何属性。

<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
    }
}

4 个答案:

答案 0 :(得分:18)

在处理程序中,this是表格单元格,因此对于单元格索引,请执行以下操作:

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

并且对于行索引,请执行以下操作:

var rowIndex = this.parentNode.rowIndex + 1;

示例: http://jsfiddle.net/fwZTc/1/

答案 1 :(得分:2)

此脚本块将为您提供所需的信息,方法是将信息作为属性添加到单元格,然后在onclick函数中访问它们:

// 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)");
        };
    }
}

答案 2 :(得分:1)

好吧,当你有rowspan / colspan时,你可以获得更多乐趣,但是,如果网格是常规的,你可以通过这样做来确定你在索引中的位置:

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

答案 3 :(得分:0)

使用&#34;这个&#34;在细胞表

&#13;
&#13;
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;
      }
&#13;
tr, th, td {
  padding: 0.6rem;
  border: 1px solid black
}

table:hover {
  cursor: pointer;
}
&#13;
<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>
&#13;
&#13;
&#13;