<div class="codetitle"> <span><a style="CURSOR: pointer" data="18233" class="copybut" id="copybut18233" onclick="doCopy('code18233')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code18233"> <br><br><br><br> <title></title> <br> <script language="javascript" type="text/javascript"><BR> function addRow() {<BR> var newTR = document.getElementById("testTable").insertRow(document.getElementById("testTable").rows.length);<BR> var newNameTD = newTR.insertCell(0);<BR> newNameTD.innerHTML = "aa";<BR> var newNameTD = newTR.insertCell(1);<BR> newNameTD.innerHTML = "<input name='LastName' id='LastName' type='text' />";<BR> var newNameTD = newTR.insertCell(2);<BR> newNameTD.innerHTML = "<input name='BirthDay' id='BirthDay' type='text' />";<BR> var newNameTD = newTR.insertCell(3);<BR> newNameTD.innerHTML = "<input name='age' id='age' type='text' />";<BR> }<BR> function insertRow() {<BR> var oTable = document.getElementById("table_info");<BR> var oTr = oTable.insertRow();<BR> var oTd = oTr.insertCell();<BR> oTd.innerHTML = "新添加了一行";<BR> }<BR> function insertRow2() {<BR> //获取table对象<BR> var table = document.getElementById("table_info2");<BR> //找到要添加button的td,这里以表格第一行第一列为例子<BR> var oTd = table.rows[0].cells[0];<BR> //添加button到td中,添加前吧td内容清空并赋予button的代码<BR> oTd.innerHTML = "<button onclick='insertRow22()'>添加收费";<BR> }<BR> function insertRow22() {<BR> var oTable = document.getElementById("table_info2");<BR> var oTr = oTable.insertRow();<BR> var oTd = oTr.insertCell();<BR> oTd.innerHTML = "新添加了一行";<BR> }<BR> function insertRow3() {<BR> //获取table对象<BR> var table = document.getElementById("table_info3");<BR> //找到要添加button的td,这里以表格第一行第一列为例子<BR> var oTd2 = table.rows[0].insertCell();<BR> oTd2.innerHTML = "&nbps;";<BR> }<BR> function tableDiv() {<BR> var maxRow = 4;<BR> var maxCol = 8;<BR> var strTbody = ["<table border='1'><tbody>"];<BR> for (var i = 0; i < maxRow; i++) {<BR> strTbody.push("<tr>");<BR> for (var j = 0; j < maxCol; j++) {<BR> strTbody.push("<td>test");<BR> }<BR> strTbody.push("");<BR> }<BR> strTbody.push("");<BR> var obj = document.getElementById("tableDiv");<BR> obj.innerHTML = strTbody.join("");<BR> }<BR> function init() {<BR> var _table, _tbody, tr, td, text, maxRow, maxCol;<BR> var docBody = document.body;<BR> var _doc = document;<BR> maxRow = 5;<BR> maxCol = 8;<BR> _table = _doc.createElement("table");<BR> _table.border = "1";<BR> _table.style.tableLayout = "fixed";<BR> _tbody = _doc.createElement("tbody");<BR> _table.insertBefore(_tbody, null);<BR> docBody.insertBefore(_table, null);<BR> for (var i = 0; i < maxRow; i++) {<BR> tr = _doc.createElement("tr");<BR> _tbody.insertBefore(tr, null);<BR> for (var j = 0; j < maxCol; j++) {<BR> td = _doc.createElement("td");<BR> text = _doc.createTextNode("Text");<BR> td.insertBefore(text, null);<BR> tr.insertBefore(td, null);<BR> }<BR> }<BR> }<BR> </script><br><br><br> <div> <br> <table id="testTable" border="1" cellspacing="1"> <br> <tr> <br> <th> <br> FirstName<br> </th> <br> <th> <br> LastName<br> </th> <br> <th> <br> BirthDay<br> </th> <br> <th> <br> age<br> </th> <br> </tr> <br> <tr> <br> <td> <br> Jim<br> </td> <br> <td> <br> Green<br> </td> <br> <td> <br> L.A<br> </td> <br> <td> <br> 23<br> </td> <br> </tr> <br> <tr> <br> <td> <br> Andrew<br> </td> <br> <td> <br> Hou<br> </td> <br> <td> <br> Xi'an<br> </td> <br> <td> <br> 25<br> </td> <br> </tr> <br> </table> <br> <input type="button" id="aaa" value="+add new Row" onclick="addRow();"><br> </div> <br> <br><br> <hr> <br> <br><br> <div> <br> <input type="button" value="按钮添加行" onclick="insertRow();"><br> <table id="table_info" border="1"> <br> <tr> <br> <th> <br> Name<br> </th> <br> <th> <br> Age<br> </th> <br> </tr> <br> </table> <br> </div> <br> <br><br> <hr> <br> <br><br> <div> <br> <input type="button" value="按钮中添加行" onclick="insertRow2();"><br> <table id="table_info2" border="1"> <br> <tr> <br> <th> <br> Name<br> </th> <br> <th> <br> Age<br> </th> <br> </tr> <br> </table> <br> </div> <br> <br><br> <hr> <br> <br><br> <div> <br> <input type="button" value="连续添加列" onclick="insertRow3();"><br> <table id="table_info3" border="1"> <br> <tr> <br> <th> <br> Name<br> </th> <br> <th> <br> Age<br> </th> <br> </tr> <br> </table> <br> </div> <br> <br><br> <hr> <br> <br><br> <div> <br> <input type="button" value="添加一个table" onclick="tableDiv();"><br> <div id="tableDiv"></div> <br> </div> <br> <br><br> <hr> <br> <br><br> <div> <br> <input type="button" value="连续添加table" onclick="init();"><br> </div> <br><br><br> </div>