获取table表格行号列号

<html>
<head>
<title> New Document </title>
</head>

<body>
<table >
<tr><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td></tr>
<tr><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td></tr>
<tr><td>aaa</td><td>aaa</td><td>aaa</td><td>aaa</td></tr>
</table>
<script language="javascript">
<!--
   function clickHandler(id){
    var t=document.getElementById(id).getElementsByTagName("td");
    var len=t.length;
    for(var i=0;i<len;i++){
     t[i].style.cursor="hand";
     if(document.addEventListener)
     {
      t[i].addEventListener("click",function(e){
       e=e.target;
       alert("行号:"+e.parentNode.rowIndex+"\n"+"列号:"+e.cellIndex);
      });
     }
     else
     {
      t[i].attachEvent("onclick",function(){
       var e=event.srcElement;
       alert("行号:"+e.parentNode.rowIndex+"\n"+"列号:"+e.cellIndex);
      });
     }
    }   
   }
   clickHandler("t");
//-->
</script>

</body>
</html>

相关推荐