在html里的代码是这样的,一张表
<body>
<table width="800" border="1" align="center" id="1">
  <tr id="11">
    <td id="111">列内容1</td>
    <td id="112">列内容2</td>
    <td id="113">列内容3</td>
  </tr>
  <tr id="21">
    <td id="211">列内容4</td>
    <td id="212">列内容4</td>
    <td id="213">列内容6</td>
  </tr>
  <tr>
    <td id="311">列内容7</td>
    <td id="312">列内容8</td>
    <td id="313">列内容9</td>
  </tr>
</table>
</body>
如何写一个JS函数遍历表格列的内容?谢谢。

解决方案 »

  1.   


    var tb=document.getElementById("1"),len=tb.rows.length,row;
    for(var i=0;i<len;i++){
        row=tb.rows[i];
        for(var j=0;j<row.cells.length;j++){
            alert(row.cells[j].innerHTML)
        }
    }
    //随手写的,试试
      

  2.   


    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script>
    function changeColor()
    {
    var tab=document.getElementById("1");
    var rowsnum=tab.rows.length;
    alert(rowsnum);
    for(var i=0;i<rowsnum;i++)
    {
    for(var j=0;j<3;j++)
    alert(tab.rows[i].cells[j].innerText);
    }
    }

    </script>
    </head><body>
    <table width="800" border="1" align="center" id="1">
      <tr id="11">
      <td id="111">列内容1</td>
      <td id="112">列内容2</td>
      <td id="113">列内容3</td>
      </tr>
      <tr id="21">
      <td id="211">列内容4</td>
      <td id="212">列内容4</td>
      <td id="213">列内容6</td>
      </tr>
      <tr>
      <td id="311">列内容7</td>
      <td id="312">列内容8</td>
      <td id="313">列内容9</td>
      </tr>
    </table>
    <input type="button"  onclick="changeColor()"  value="test"/>
    </body>
    </html>
      

  3.   


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script>
    function changeColor(obj)
    {
     alert(obj.innerText);
    }
    </script>
    </head><body>
    <table width="800" border="1" align="center" id="1">
      <tr id="11">
      <td id="111"  onclick="changeColor(this)">列内容1</td>
      <td id="112"  onclick="changeColor(this)">列内容2</td>
      <td id="113"  onclick="changeColor(this)">列内容3</td>
      </tr>
      <tr id="21">
      <td id="211"  onclick="changeColor(this)">列内容4</td>
      <td id="212"  onclick="changeColor(this)">列内容4</td>
      <td id="213"  onclick="changeColor(this)">列内容6</td>
      </tr>
      <tr>
      <td id="311"  onclick="changeColor(this)">列内容7</td>
      <td id="312"  onclick="changeColor(this)">列内容8</td>
      <td id="313"  onclick="changeColor(this)">列内容9</td>
      </tr>
    </table>
    <input type="button"  onclick="changeColor(this)"  value="test"/>
    </body>
    </html>
    倒  lz你把问题说清楚点阿 
      

  4.   

    这里点击下test,弹出来的是空的呢。
      

  5.   

    要是我只点击下test然后列出所有单元格的内容该是怎么改呢?不过,我觉得楼主的js蛮强的,谢了
      

  6.   

    lz你太逗了~~<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script>
    function change()
    {
    var tab=document.getElementById("1");
    var rowsnum=tab.rows.length;
    var str="";
    for(var i=0;i<rowsnum;i++)
    {
    for(var j=0;j<3;j++)
     str+=tab.rows[i].cells[j].innerText+"\r\n";
    }
    alert(str);
    }
    function changeColor(obj)
    {
     alert(obj.innerText);
    }

    </script>
    </head><body>
    <table width="800" border="1" align="center" id="1">
      <tr id="11">
      <td id="111"  onclick="changeColor(this)">列内容1</td>
      <td id="112"  onclick="changeColor(this)">列内容2</td>
      <td id="113"  onclick="changeColor(this)">列内容3</td>
      </tr>
      <tr id="21">
      <td id="211"  onclick="changeColor(this)">列内容4</td>
      <td id="212"  onclick="changeColor(this)">列内容4</td>
      <td id="213"  onclick="changeColor(this)">列内容6</td>
      </tr>
      <tr>
      <td id="311"  onclick="changeColor(this)">列内容7</td>
      <td id="312"  onclick="changeColor(this)">列内容8</td>
      <td id="313"  onclick="changeColor(this)">列内容9</td>
      </tr>
    </table><input type="button"  onclick="change()"  value="test"/>
    </body>
    </html>
      

  7.   

    <script>
    var a = document.getElementsByTagName("td");
    for(var i=0; i<a.length; i++){
      alert(a[i].innerHTML) ;
    }

    </script>
      

  8.   

    xiaofan_sap楼主好强!谢谢了。在公司还遇到一个js问题,楼主能否告之qq,不胜感激。
      

  9.   

    不需要循环,更不用嵌套循环,var table= document.getElementById("1");alert(table.textContent || table.innerText)
      

  10.   


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script>
    function changeColor(obj)
    {
     alert(obj.innerText);
    }
    </script>
    </head><body>
    <table width="800" border="1" align="center" id="tab">
      <tr id="11">
      <td id="111"  onclick="changeColor(this)">列内容1</td>
      <td id="112"  onclick="changeColor(this)">列内容2</td>
      <td id="113"  onclick="changeColor(this)">列内容3</td>
      </tr>
      <tr id="21">
      <td id="211"  onclick="changeColor(this)">列内容4</td>
      <td id="212"  onclick="changeColor(this)">列内容4</td>
      <td id="213"  onclick="changeColor(this)">列内容6</td>
      </tr>
      <tr>
      <td id="311"  onclick="changeColor(this)">列内容7</td>
      <td id="312"  onclick="changeColor(this)">列内容8</td>
      <td id="313"  onclick="changeColor(this)">列内容9</td>
      </tr>
    </table>
    <input type="button"  onclick="javascript:alert(document.getElementById('tab').innerHTML.replace(/<[^>]*>/g,''))"  value="test"/>
    </body>
    </html>这样看的清楚些,如果不需要换行的话:<input type="button"  onclick="javascript:alert(document.getElementById('tab').innerHTML.replace(/<[^>]*>|[\n\r]*/g,''))" value="test"/>
      

  11.   

    学习中 access 被独占怎么解决啊 
      

  12.   

    这个已近视XML文件,如果做成标准的XML文件(加上第一行头文件,就更好遍历了.
      

  13.   

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script>
    function changeColor()
    {
    var tab=document.getElementById("1");
    var rowsnum=tab.rows.length;
    alert(rowsnum);
    for(var i=0;i<rowsnum;i++)
    {
        for(var j=0;j<3;j++)
        alert(tab.rows[i].cells[j].innerText);
        }
    }
        
    </script>
    </head><body>
    <table width="800" border="1" align="center" id="1">
      <tr id="11">
      <td id="111">列内容1</td>
      <td id="112">列内容2</td>
      <td id="113">列内容3</td>
      </tr>
      <tr id="21">
      <td id="211">列内容4</td>
      <td id="212">列内容4</td>
      <td id="213">列内容6</td>
      </tr>
      <tr>
      <td id="311">列内容7</td>
      <td id="312">列内容8</td>
      <td id="313">列内容9</td>
      </tr>
    </table>
    <input type="button"  onclick="changeColor()"  value="test"/>
    </body>
    </html>