<table border=1>
<tr><td onclick=alert("Cell:"+this.cellIndex);alert("Row:"+this.parentElement.rowIndex)>123123</td><td onclick=alert("Cell:"+this.cellIndex);alert("Row:"+this.parentElement.rowIndex)>123123</td></tr>
<tr><td onclick=alert("Cell:"+this.cellIndex);alert("Row:"+this.parentElement.rowIndex)>123123</td><td onclick=alert("Cell:"+this.cellIndex);alert("Row:"+this.parentElement.rowIndex)>123123</td></tr>
<tr><td onclick=alert("Cell:"+this.cellIndex);alert("Row:"+this.parentElement.rowIndex)>123123</td><td onclick=alert("Cell:"+this.cellIndex);alert("Row:"+this.parentElement.rowIndex)>123123</td></tr>
</table>

解决方案 »

  1.   

    写简单一点吧<script>
    function get(obj){
    alert("Cell:"+obj.cellIndex+"\nRow:"+obj.parentElement.rowIndex)
    }
    </script>
    <table border=1>
    <tr><td onclick=get(this)>123123</td><td onclick=get(this)>123123</td></tr>
    <tr><td onclick=get(this)>123123</td><td onclick=get(this)>123123</td></tr>
    <tr><td onclick=get(this)>123123</td><td onclick=get(this)>123123</td></tr>
    </table>
      

  2.   

    如果表格较大,可以参考下面的写法:
    <table border=1 onclick="getCell()">
    <tr>
    <td>row:0 col:0</td><td>row:0 col:1</td></tr>
    <td>row:1 col:0</td><td>row:1 col:1</td></tr>
    <td>row:2 col:0</td><td>row:2 col:1</td></tr>
    </table>
    <script language="JavaScript">
    function getCell(){
     var e=event.srcElement;
     if(e.tagName=="TD"){
      alert("Row:" + e.parentElement.rowIndex + " Col:" + e.cellIndex);
     }
    }
    </script>
      

  3.   

    我是不希望用事件响应来做的,用事件响应的话就很容易得到td的obj了。我是希望通过类似于tdObj.setExpresstion("innerHTML","myFunction()");的方法,实现表格项的内容自动调整(比如增加删除行)。
    我想应该可以实现,自己来得到所在表格的位置的方法的吧,而不是通过传递参数。
      

  4.   

    <script>
    document.onclick=function(){
    if(event.srcElement.tagName=="TD")
    alert("Cell:"+event.srcElement.cellIndex+"\nRow:"+event.srcElement.parentElement.rowIndex)
    }
    </script>
    <table border=1>
    <tr><td>123123</td><td>123123</td></tr>
    <tr><td>123123</td><td>123123</td></tr>
    <tr><td>123123</td><td>123123</td></tr>
    </table>
      

  5.   

    <style>
    td{qswh:expression(this.innerText='r='+this.parentElement.rowIndex+' ;l='+this.cellIndex)}
    </style>
    <TABLE id="tb1" border>
    <TR>
    <TD></TD>
    <TD></TD>
    </TR>
    <TR>
    <TD></TD>
    <TD></TD>
    </TR>
    </TABLE>
      

  6.   

    <table border=1>
    <tr><td></td><td></td></tr>
    <tr><td></td><td></td></tr>
    <tr><td></td><td></td></tr>
    </table>
    <script>
    var thetd=document.all.tags("TD")
    for(i=0;i<thetd.length;i++)
    thetd[i].innerHTML="Cell:"+thetd[i].cellIndex+"<br>Row:"+thetd[i].parentElement.rowIndex
    </script>