zoukankan      html  css  js  c++  java
  • js操作html的table,包括添加行,添加列,删除行,删除列,合并单元格

    html部分 写道

    <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>

    生成表格,采用appendChild 写道

    function init(){
    _table=document.getElementById ("table");
    _table.border="1px";
    _table.width="800px";
    for(var i=1;i<10;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);
    }
    }

    添加行,使用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);
    }

    添加行的另一种方法insertRow 写道

    function addRow_withInsert(){
    varrow=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) 写道

    /*删除行,采用deleteRow(row Index)*/
    function removeRow(){
    /* var row=document.getElementById("2");
    var index=row.rowIndex;
    alert(index);*/
    document.getElementById("newbody").deleteRow(document.getElementById(document.getElementById("table").rows.length).rowIndex);
    }

    添加列,采用insertCell(列位置)方法 写道

    function addCell(){
    /*document.getElementById("table").rows.item(0).cells.length
    用来获得表格的列数
    */
    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(列位置)的方法 写道

    /*删除列,采用deleteCell(列位置)的方法*/
    function removeCell(){
    for(var i=0;i<document.getElementById("table").rows.length;i++){
    document.getElementById("table").rows[i].deleteCell(0);
    }
    }

    合并单元格(未实现) 写道

    我的代码有问题,主要是表格会乱掉,一直没有改好
    function rebulid(){
    var beginRow=document.getElementById("beginRow").value;/*开始行*/
    var endRow=document.getElementById("endRow").value;/*结束行*/
    var beginCol=document.getElementById("beginCol").value;/*开始列*/
    var endCol=document.getElementById("endCol").value;/*结束列*/
    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;
    }

    联盟快卖 商人,生意人,待创业人士在此可以共赢互利 期待你的加入 群号:140809277
  • 相关阅读:
    ABS
    Windows Internals 6th Security
    Windows Internals 6th chap4 services
    Windows Internals 6th chap5 Thread
    Python 腾讯云短信,发送手机验证码
    Python 阿里大于发送手机验证码
    爬虫新手学习2-爬虫进阶(urllib和urllib2 的区别、url转码、爬虫GET提交实例、批量爬取贴吧数据、fidder软件安装、有道翻译POST实例、豆瓣ajax数据获取)
    爬虫新手学习1-爬虫基础
    Python Django CMDB项目实战之-3创建form表单,并在前端页面上展示
    Python Django CMDB项目实战之-2创建APP、建模(models.py)、数据库同步、高级URL、前端页面展示数据库中数据
  • 原文地址:https://www.cnblogs.com/yexinw/p/2006860.html
Copyright © 2011-2022 走看看