JavaScript動態(tài)操作表格實例(添加,刪除行,列及單元格)
更新時間:2013年11月25日 10:29:34 作者:
這篇文章主要是對JavaScript動態(tài)操作表格實例(添加,刪除行,列及單元格)進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助
復制代碼 代碼如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>js動態(tài)操作表格</title>
<script language="javascript">
function init(){
_table=document.getElementById("table");
_table.border="1px";
_table.width="800px";
for(var i=1;i<6;i++){
var row=document.createElement("tr");
row.id=i;
for(var j=1;j<6;j++){
var cell=document.createElement("td");
cell.id=i+"/"+j;
cell.appendChild(document.createTextNode("第"+cell.id+"列"));
row.appendChild(cell);
}
document.getElementById("newbody").appendChild(row);
}
}
function rebulid(){
var beginRow=document.getElementById("beginRow").value;/*開始行*/
var endRow=document.getElementById("endRow").value;/*結(jié)束行*/
var beginCol=document.getElementById("beginCol").value;/*開始列*/
var endCol=document.getElementById("endCol").value;/*結(jié)束列*/
var tempCol=beginRow+"/"+beginCol;/*定位要改變屬性的列*/
alert(tempCol);
var td=document.getElementById(tempCol);
for(var x=beginRow;x<=endRow;x++){
for(var i=beginCol;i<=endCol;i++){
if(x==beginRow){
document.getElementById("table").rows[x].deleteCell(i+1);
}
else{
document.getElementById("table").rows[x].deleteCell(i);
}
}
}
td.rowSpan=(endRow-beginRow)+1;
}
/*添加行,使用appendChild方法*/
function addRow(){
var length=document.getElementById("table").rows.length;
/*document.getElementById("newbody").insertRow(length);
document.getElementById(length+1).setAttribute("id",length+2);*/
var tr=document.createElement("tr");
tr.id=length+1;
var td=document.createElement("td");
for(i=1;i<4;i++){
td.id=tr.id+"/"+i;
td.appendChild(document.createTextNode("第"+td.id+"列"));
tr.appendChild(td);
}
document.getElementById("newbody").appendChild(tr);
}
function addRow_withInsert(){
var row=document.getElementById("table").insertRow(document.getElementById("table").rows.length);
var rowCount=document.getElementById("table").rows.length;
var countCell=document.getElementById("table").rows.item(0).cells.length;
for(var i=0;i<countCell;i++){
var cell=row.insertCell(i);
cell.innerHTML="新"+(rowCount)+"/"+(i+1)+"列";
cell.id=(rowCount)+"/"+(i+1);
}
}
/*刪除行,采用deleteRow(row Index)*/
function removeRow(){
document.getElementById("newbody").deleteRow(document.getElementById(document.getElementById("table").rows.length).rowIndex);
}
/*添加列,采用insertCell(列位置)方法*/
function addCell(){
/*document.getElementById("table").rows.item(0).cells.length
用來獲得表格的列數(shù)
*/
for(var i=0;i<document.getElementById("table").rows.length;i++){
var cell=document.getElementById("table").rows[i].insertCell(2);
cell.innerHTML="第"+(i+1)+"/"+3+"列";
}
}
/*刪除列,采用deleteCell(列位置)的方法*/
function removeCell(){
for(var i=0;i<document.getElementById("table").rows.length;i++){
document.getElementById("table").rows[i].deleteCell(0);
}
}
</script>
</head>
<body onLoad="init();">
<table id="table" align="center">
<tbody id="newbody"></tbody>
</table>
<div>
<table width="800px" border="1px" align="center">
<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>
<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>
<tr><td align="center" colspan="2"><input type="button" id="addRows" name="addRows" onClick="addRow_withInsert();" value="添加行"/></td></tr>
</table>
</div>
<div>
<table width="800px" border="1px" align="center">
<tr><td>從第<input type="text" id="beginRow" name="beginRow" value=""/>行到<input type="text" name="endRow" id="endRow" value=""/>行</td><td rowspan="2" id="test"><input type="button" name="hebing" id="hebing" value="合并" onClick="rebulid();"/></td></tr>
<tr><td>從第<input type="text" name="beginCol" id="beginCol" value=""/>列到<input type="text" name="endCol" id="endCol" value=""/>列</td></tr>
</table>
</div>
</body>
</html>
您可能感興趣的文章:
- JS/jQuery實現(xiàn)超簡單的Table表格添加,刪除行功能示例
- js動態(tài)添加表格逐行添加、刪除、遍歷取值的實例代碼
- js實現(xiàn)添加刪除表格(兩種方法)
- JS實現(xiàn)動態(tài)表格的添加,修改,刪除功能(推薦)
- 基于JavaScript實現(xiàn)動態(tài)添加刪除表格的行
- 原生JS和JQuery動態(tài)添加、刪除表格行的方法
- js簡單的表格添加行和刪除行操作示例
- 動態(tài)添加刪除表格行的js實現(xiàn)代碼
- JS小功能(操作Table--動態(tài)添加刪除表格及數(shù)據(jù))實現(xiàn)代碼
- js動態(tài)實現(xiàn)表格添加和刪除操作

Bootstrap基本插件學習筆記之Tooltip提示工具(18)
這篇文章主要為大家詳細介紹了Bootstrap基本插件學習筆記之oltip提示工具的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
2016-12-12 
怎樣使用?JavaScript?轉(zhuǎn)義字符串中的引號
要轉(zhuǎn)義字符串中的單引號或雙引號,需要在字符串內(nèi)容中的每個單引號或雙引號之前使用反斜杠?\?字符,例如?‘that’s?it’,這篇文章主要介紹了如何使用?JavaScript?轉(zhuǎn)義字符串中的引號,需要的朋友可以參考下
2023-11-11 
原生javascript實現(xiàn)addClass,removeClass,hasClass函數(shù)
這篇文章主要介紹了原生javascript實現(xiàn)addClass,removeClass,hasClass函數(shù)的相關(guān)代碼,有需要的小伙伴可以參考下
2016-02-02