I choose , I like…………
要走好明天的路,必须记住昨天走过的路,思索今天正在走着的路。

怎样获取预定义的table的行号和列号呢?很简单,只需要了解 table 的几个属性值用法即可。Table的总行数可以通过标记“tr 得到,但是table的列数却不能直接获取,它需要借助 rows 通过标记“th 和“td”得到。

为了更直观,下面以示例来说明如何得到行号和列号。点击 Id 'MyTable' 的表格的任意单元时,返回该单元的行号和列号!示例代码如下:

       function  getRowAndColumn()

       {

              if (!document.getElementsByTagName || !document.createTextNode) return;

              var rows = document.getElementById('MyTable').getElementsByTagName('tr');

              var cols;

              for (i = 0; i < rows.length; i++)

              {

                     rows[i].onclick = function()

                     {

                            alert(":"+eval(this.rowIndex + 1));

                     }

                     if (i=0)

                     {

                            colsTH = rows[i].getElementsByTagName('th');

                            alert(colsTH.length);

                            for (k = 0; k< colsTH.length; k++)

                            {

                                   colsTH[k].onclick = function()

                                   {

                                          alert(":"+eval(this.cellIndex +1));

                                   }

                            }

                     }

                     else

                     {

                            cols = rows[i].getElementsByTagName('td');

                            for (j = 0; j < cols.length; j++)

                            {

                                   cols[j].onclick = function()

                                   {

                                          alert(":"+eval(this.cellIndex + 1));

                                   }

                            }

                     }

              }

       }

(注:关键代码已经加黑)

posted on 2008-09-26 20:18  飞翔的Angela  阅读(8711)  评论(5编辑  收藏  举报