讀取table內容的javascript代碼_javascript技巧

來源:互聯網
上載者:User
<html> <head> <meta http-equiv="Content-Language" content="zh-cn"> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>222222 </title> </head> <body> <table border="1" width="45%" height="29" id="table1"> <tr> <td> <input type="checkbox" name="C1" value="ON"> </td> <td>雲棲社區</td> <td>www.jb51.net</td> </tr> <tr> <td> <input type="checkbox" name="C2" value="ON"> </td> <td>百度</td> <td>www.baidu.com</td> </tr> <tr> <td> <input type="checkbox" name="C3" value="ON"> </td> <td>伺服器軟體</td> <td>s.jb51.net</td> </tr> </table> <input type="button" onclick="readArray()" value="取得表格值"> <script> //使用數組取得表格值 function getRowText() { var arr = new Array(); var count = 0; for(var i=0;i <table1.rows.length;i++) { var row = table1.rows[i]; //取得CHECKBOX var objCheck = row.cells[0].getElementsByTagName("INPUT")[0]; //如果CHECKBOX被選中.添加到二維數組 if(objCheck!=null && objCheck.checked) { //arr[i]= row.innerText ; arr[count] = new Array(); for(var x=0;x <row.cells.length;x++) { //不計單選框的儲存格內容 if(x==0) continue; arr[count][x-1] = row.cells[x].innerText; } count++; } } return arr; } //讀取表格值 function readArray() { var arr = getRowText(); for(var i=0;i <arr.length;i++) { //取得每一行的值 for(var x= 0;x <arr[i].length;x++) { document.write(arr[i][x]); } } } </script> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]


定義和用法
rows 集合返回表格中所有行(TableRow 對象)的一個數組,即一個 HTMLCollection。

該集合包括 <thead>、<tfoot> 和 <tbody> 中定義的所有行。

文法
tableObject.rows[]執行個體
下面的例子使用了 rows 集合來顯示第一行中的內容:
<html> <head> <script type="text/javascript"> function showRow() { alert(document.getElementById('myTable').rows[0].innerHTML) } </script> </head> <body> <table id="myTable" border="1"> <tr> <td>Row1 cell1</td> <td>Row1 cell2</td> </tr> <tr> <td>Row2 cell1</td> <td>Row2 cell2</td> </tr> </table> <input type="button" onclick="showRow()" value="Show innerHTML"> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.