這篇文章主要介紹了javascript動態創建表格及添加數據,以實例形式分析了javascript動態創建表格的常用方法,包括不兼容IE6與兼容IE6的實現方法,非常具有實用價值,需要的朋友可以參考下
本文實例講述了javascript動態創建表格及添加數據的方法。分享給大家供大家參考。具體分析如下:
1. 動態創建表格(代碼不兼容IE6)
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>動態創建表格</title>
- <script type="text/javascript">
- function AppendTableData() {
- var table = document.getElementById("tblMain");
- var data = { "百度": "http://www.baidu.com",
- "武林網": "http://www.49028c.com",
- "搜狐": "http://www.sohu.com"
- };
- for (var key in data) {
- var tr = document.createElement("tr");
- var td1 = document.createElement("td");
- td1.innerText = key;
- //FireFox不支持innerText,只能使用innerHTML
- tr.appendChild(td1);
- var td2 = document.createElement("td");
- td2.innerHTML = "<a href='" + data[key] + "'>" + data[key] + "</a>";
- tr.appendChild(td2);
- table.appendChild(tr);
- }
- }
- </script>
- </head>
- <body>
- <table id="tblMain"></table>
- <input type="button" value="動態添加網格數據"
- onclick="AppendTableData()" />
- </body>
- </html>
2. 動態創建表格(兼容IE6、IE7)
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>添加網格數據(處理了IE兼容問題)</title>
- <script type="text/javascript">
- function AppendData() {
- var data = {"武林網":"http://www.49028c.com",
- "百度":"http://www.baidu.com",
- "搜狐": "http://www.sohu.com"};
- var table = document.getElementById("tblMain");
- for (var key in data) {
- var value = data[key];
- var tr = table.insertRow(-1);
- //FireFox必須使用-1這個參數
- var td1 = tr.insertCell(-1);
- td1.innerText = key;
- var td2 = tr.insertCell(-1);
- td2.innerHTML = "<a href='" + value + "'>" + value + "</a>";
- }
- }
- </script>
- </head>
- <body>
- <table border="1" id="tblMain"></table>
- <input type="button" value="添加網格數據(處理了IE兼容問題)"
- onclick="AppendData()" />
- </body>
- </html>
3. 動態創建表格(兼容IE6、IE7)
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>動態創建表格(處理IE6兼容問題)</title>
- <script type="text/javascript">
- function AppendTableData() {
- var table = document.getElementById("tblMain");
- var data = { "百度": "http://www.baidu.com",
- "武林網": "http://www.49028c.com",
- "搜狐": "http://www.sohu.com"
- };
- for (var key in data) {
- var tr = document.createElement("tr");
- var td1 = document.createElement("td");
- td1.innerText = key;
- tr.appendChild(td1);
- var td2 = document.createElement("td");
- td2.innerHTML = "<a href='" + data[key] + "'>" + data[key] + "</a>";
- tr.appendChild(td2);
- //table.appendChild(tr); 把這一句替換掉
- table.tBodies[0].appendChild(tr);
- }
- }
- </script>
- </head>
- <body>
- <!--由于動態添加的數據在debugbar中看生成的HTML代碼發現,
- 會自動添加一個<tbody>
- 并且內容是空的,把我們動態生成的數據給沖掉了,
- 所以我們手工添加一個<tbody>
- tr添加到這個<tbody>下面
- -->
- <table id="tblMain"><tbody></tbody></table>
- <input type="button" value="動態添加網格數據"
- onclick="AppendTableData()" />
- </body>
- </html>
希望本文所述對大家的javascript程序設計有所幫助。
新聞熱點
疑難解答
圖片精選