這篇文章主要介紹了javascript動態(tài)創(chuàng)建表格及添加數(shù)據(jù),以實(shí)例形式分析了javascript動態(tài)創(chuàng)建表格的常用方法,包括不兼容IE6與兼容IE6的實(shí)現(xiàn)方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了javascript動態(tài)創(chuàng)建表格及添加數(shù)據(jù)的方法。分享給大家供大家參考。具體分析如下:
1. 動態(tài)創(chuàng)建表格(代碼不兼容IE6)
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>動態(tài)創(chuàng)建表格</title>
- <script type="text/javascript">
- function AppendTableData() {
- var table = document.getElementById("tblMain");
- var data = { "百度": "http://www.baidu.com",
- "武林網(wǎng)": "http://www.survivalescaperooms.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="動態(tài)添加網(wǎng)格數(shù)據(jù)"
- onclick="AppendTableData()" />
- </body>
- </html>
2. 動態(tài)創(chuàng)建表格(兼容IE6、IE7)
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>添加網(wǎng)格數(shù)據(jù)(處理了IE兼容問題)</title>
- <script type="text/javascript">
- function AppendData() {
- var data = {"武林網(wǎng)":"http://www.survivalescaperooms.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這個(gè)參數(shù)
- 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="添加網(wǎng)格數(shù)據(jù)(處理了IE兼容問題)"
- onclick="AppendData()" />
- </body>
- </html>
3. 動態(tài)創(chuàng)建表格(兼容IE6、IE7)
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>動態(tài)創(chuàng)建表格(處理IE6兼容問題)</title>
- <script type="text/javascript">
- function AppendTableData() {
- var table = document.getElementById("tblMain");
- var data = { "百度": "http://www.baidu.com",
- "武林網(wǎng)": "http://www.survivalescaperooms.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>
- <!--由于動態(tài)添加的數(shù)據(jù)在debugbar中看生成的HTML代碼發(fā)現(xiàn),
- 會自動添加一個(gè)<tbody>
- 并且內(nèi)容是空的,把我們動態(tài)生成的數(shù)據(jù)給沖掉了,
- 所以我們手工添加一個(gè)<tbody>
- tr添加到這個(gè)<tbody>下面
- -->
- <table id="tblMain"><tbody></tbody></table>
- <input type="button" value="動態(tài)添加網(wǎng)格數(shù)據(jù)"
- onclick="AppendTableData()" />
- </body>
- </html>
希望本文所述對大家的javascript程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選