<div class="codetitle"> <span><a style="CURSOR: pointer" data="37101" class="copybut" id="copybut37101" onclick="doCopy('code37101')"><u>複製程式碼</u></a></span> 程式碼如下:</div> <div class="codebody" id="code37101"> <br><html><br><head><br><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><br><title>js动态操作表格</title><br><script language="javascript"><br>function init(){<br>_table=document.getElementById("table");<br>_table.border="1px";<br>_table.width="800px";<br>for(var i=1;i<6;i++){<BR>var row=document.createElement("tr");<BR>row.id=i;<BR>for(var j=1;j<6;j++){<BR>var cell=document.createElement("td");<BR>cell.id=i+"/"+j;<BR>cell.appendChild(document.createTextNode("第"+cell.id+"列"));<BR>row.appendChild(cell);<BR>}<BR>document.getElementById("newbody").appendChild(row);<BR>}<BR>}<BR>function rebulid(){<BR>var beginRow=document.getElementById("beginRow").value;/*开始行*/<BR>var endRow=document.getElementById("endRow").value;/*结束行*/<BR>var beginCol=document.getElementById("beginCol").value;/*开始列*/<BR>var endCol=document.getElementById("endCol").value;/*结束列*/<BR>var tempCol=beginRow+"/"+beginCol;/*定位要改变属性的列*/<BR>alert(tempCol);<BR>var td=document.getElementById(tempCol);<BR>for(var x=beginRow;x<=endRow;x++){<BR>for(var i=beginCol;i<=endCol;i++){<BR>if(x==beginRow){<BR>document.getElementById("table").rows[x].deleteCell(i+1);<BR>}<BR>else{<BR>document.getElementById("table").rows[x].deleteCell(i);<BR>}<BR>}<BR>}<BR>td.rowSpan=(endRow-beginRow)+1;<BR>}<BR>/*添加行,使用appendChild方法*/<BR>function addRow(){<BR>var length=document.getElementById("table").rows.length;<BR>/*document.getElementById("newbody").insertRow(length);<BR>document.getElementById(length+1).setAttribute("id",length+2);*/<BR>var tr=document.createElement("tr");<BR>tr.id=length+1;<BR>var td=document.createElement("td");<BR>for(i=1;i<4;i++){<BR>td.id=tr.id+"/"+i;<BR>td.appendChild(document.createTextNode("第"+td.id+"列"));<BR>tr.appendChild(td);<BR>}<BR>document.getElementById("newbody").appendChild(tr);<BR>}<BR>function addRow_withInsert(){<BR>var row=document.getElementById("table").insertRow(document.getElementById("table").rows.length);<BR>var rowCount=document.getElementById("table").rows.length;<BR>var countCell=document.getElementById("table").rows.item(0).cells.length;<BR>for(var i=0;i<countCell;i++){<BR>var cell=row.insertCell(i);<BR>cell.innerHTML="新"+(rowCount)+"/"+(i+1)+"列";<BR>cell.id=(rowCount)+"/"+(i+1);<BR>}<BR>}<BR>/*删除行,采用deleteRow(row Index)*/<BR>function removeRow(){<BR>document.getElementById("newbody").deleteRow(document.getElementById(document.getElementById("table").rows.length).rowIndex);<BR>}<BR>/*添加列,采用insertCell(列位置)方法*/<BR>function addCell(){<BR>/*document.getElementById("table").rows.item(0).cells.length<BR>用来获得表格的列数<BR>*/<BR>for(var i=0;i<document.getElementById("table").rows.length;i++){<BR>var cell=document.getElementById("table").rows[i].insertCell(2);<BR>cell.innerHTML="第"+(i+1)+"/"+3+"列";<BR>}<BR>}<BR>/*删除列,采用deleteCell(列位置)的方法*/<BR>function removeCell(){<BR>for(var i=0;i<document.getElementById("table").rows.length;i++){<BR>document.getElementById("table").rows[i].deleteCell(0);<BR>}<BR>}<BR></script><br></head><br><body onLoad="init();"><br><table id="table" align="center"><br><tbody id="newbody"></tbody><br></table><br><div><br><table width="800px" border="1px" align="center"><br><tr><td align="center"><input type="button" id="addRow" name="addRow" onClick="addRow();" value="添加行"/></td><td align="center"><input type="button" id="delRow" name="delRow" onClick="removeRow();" value="删除行"/></td></tr><br> <tr><td align="center"><input type="button" id="delCell" name="delCell" onClick="removeCell();" value="删除列"/></td><td align="center"><input type="button" id="addCell" name="addCell" onClick=" addCell();" value="添加列"/></td></tr><br> <tr><td align="center" colspan="2"><input type="button" id="addRows" name="addRows" onClick="addRow_withInsert();" value="添加行"/></td></tr><br></table><br></div><br><div> <br><br><tr> <td>從第行到<input type="text" name="endRow" id="endRow" value="">行 td></td> <td rowspan="2 " id="test"><input type="button" name="hebing" id="hebing" value="合併" onclick="rebulid();"></td> </tr> <br><td>從第<input type="text" name="beginCol" id="beginCol" value="">列到<input type="text" name="endCol" id=" endCol" value="">列</td> <br><br><br> </div> </div>