html js 删除单元格,js操作html的table,包括添加行,添加列,删除行,删除列,合并单元格(未实现)...

本文介绍如何使用JavaScript操作HTML表格元素,包括生成表格、添加及删除行与列、合并单元格等常见操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HTML PUBLIC "-//w3c//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

js操作表格/*生成表格,采用appendChild*/functioninit(){

_table=document.getElementById("table");

_table.border="1px";

_table.width="800px";for(vari=1;i<10;i++){varrow=document.createElement("tr");

row.id=i;for(varj=1;j<6;j++){varcell=document.createElement("td");

cell.id=i+"/"+j;

cell.appendChild(document.createTextNode("第"+cell.id+"列"));

row.appendChild(cell);

}

document.getElementById("newbody").appendChild(row);

}

}functionrebulid(){varbeginRow=document.getElementById("beginRow").value;/*开始行*/varendRow=document.getElementById("endRow").value;/*结束行*/varbeginCol=document.getElementById("beginCol").value;/*开始列*/varendCol=document.getElementById("endCol").value;/*结束列*/vartempCol=beginRow+"/"+beginCol;/*定位要改变属性的列*/alert(tempCol);vartd=document.getElementById(tempCol);/*删除要合并的单元格*/for(varx=beginRow;x<=endRow;x++){for(vari=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方法*/functionaddRow(){varlength=document.getElementById("table").rows.length;/*document.getElementById("newbody").insertRow(length);

document.getElementById(length+1).setAttribute("id",length+2);*/vartr=document.createElement("tr");

tr.id=length+1;vartd=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);

}functionaddRow_withInsert(){varrow=document.getElementById("table").insertRow(document.getElementById("table").rows.length);varrowCount=document.getElementById("table").rows.length;varcountCell=document.getElementById("table").rows.item(0).cells.length;for(vari=0;i

cell.innerHTML="新"+(rowCount)+"/"+(i+1)+"列";

cell.id=(rowCount)+"/"+(i+1);

}

}/*删除行,采用deleteRow(row Index)*/functionremoveRow(){/*var row=document.getElementById("2");

var index=row.rowIndex;

alert(index);*/document.getElementById("newbody").deleteRow(document.getElementById(document.getElementById("table").rows.length).rowIndex);

}/*添加列,采用insertCell(列位置)方法*/functionaddCell(){/*document.getElementById("table").rows.item(0).cells.length

用来获得表格的列数*/for(vari=0;i

cell.innerHTML="第"+(i+1)+"/"+3+"列";

}

}/*删除列,采用deleteCell(列位置)的方法*/functionremoveCell(){for(vari=0;i

document.getElementById("table").rows[i].deleteCell(0);

}

}

从第行到行从第列到列
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值