zoukankan      html  css  js  c++  java
  • js创建表格

    <!-- 动态生成表格 ,删除 修改-->
    <html>
    <head>
    <meta content="text/html;charset=utf-8">
    <script type="text/javascript">
    var ary=new Array();
    ary.push( new stud("1","aaa","12","B"));
    ary.push( new stud("2","aaa","12","B"));
    ary.push( new stud("3","aaa","12","B"));
    ary.push( new stud("4","aaa","12","B"));
    // alert(ary[0].no+" "+ary[0].name);
    var g_trobj;
    function creatTable(){
    s="<table border='2px'>";
    //表头
    s+="<tr>"+
    "<td width='100px'>&nbsp;</td>"+
    "<td width='100px'>NO</td>"+
    "<td width='100px'>NAME</td>"+
    "<td width='100px'>AGE</td>"+
    "<td width='100px'>SEX</td>"+
    "<td width='100px'>FUNCTION</td>"+
    "</tr>";
    //表的内容
    for(var i=0;i<ary.length;i++){
    var u=ary[i];
    s+="<tr>";
    s+="<td><input type='checkbox' name='T'></td>";
    s+="<td>"+u.no+"</td>"+
    "<td>"+u.name+"</td>"+
    "<td>"+u.age+"</td>"+
    "<td>"+u.sex+"</td>"+
    "<td><input type='button' value='edit' onclick='tredit(this)'>&nbsp;"+
    "<input type='button' value='delete' onclick='del(this)'></td>";
    s+="</tr>";


    }
    //画tail
    s+="<tr>"+
    "<td colspan='6'><input type='button' value='selAll' onclick='al()'>&nbsp;"+
    "<input type='button' value='unselAll' onclick='unselA()'>&nbsp;"+
    "<input type='button' value='selA' onclick='delsel()'></td>"+
    "</tr>";


    s+="</table>";
    var obj=document.getElementById("d1");
    obj.innerHTML=s;

    }

    var buobj=window.event.srcElement;
    var tdobj=buobj.parentElement;
    var trobj=td.parentElement;
    function del(e){

    for(var i=0;i<trobj.length;i++){
    if(trobj[i]==buobj){
    alert(trobj.cells[1]);
    }
    }
    }

    function delsel(){

    var checkobj=document.getElementsByName("T");
    for(var i=0;i<checkobj.length;i++){
    if(checkobj[i].checked==true){// 选中
    var e=checkobj[i];
    var tdobj=e.parentElement;
    var trobj=tdobj.parentElement; //找到选中的所在tr行
    alert(trobj.cells[1].innerText);
    }
    }
    }
    function al(){

    var checkobj=document.getElementsByName("T");
    for( var i=0;i<checkobj.length;i++){
    checkobj[i].checked=true;

    }
    }
    function unselA(){
    var checkobj=document.getElementsByName("T");
    for(var i=0;i<checkobj.length;i++){
    checkobj[i].checked=false;
    }
    }
    function tredit(obj){
    var tdobj=obj.parentElement;
    var trobj=tdobj.parentElement;
    g_trobj=trobj;
    var dobj1=document.getElementById("d1");
    alert(trobj.cells[1].innerText);

    dobj1.style.display="none";

    var dobj2=document.getElementById("d2");
    myNo.value=g_trobj.cells[1].innerText;
    myName.value=g_trobj.cells[2].innerText;
    myAge.value=g_trobj.cells[3].innerText;
    mySex.value= g_trobj.cells[4].innerText;
    dobj2.style.display="block";

    }

    function updateRow(){
    if(confirm("确定更新吗 ")!=true){return;}

    g_trobj.cells[1].innerText=myNo.value;
    g_trobj.cells[2].innerText=myName.value;
    g_trobj.cells[3].innerText=myAge.value;
    g_trobj.cells[4].innerText=mySex.value;
    var dobj1=document.getElementById("d1");
    dobj1.style.display="block";
    var dobj2=document.getElementById("d2");
    dobj2.style.display="none";

    }

    function back(){
    var dobj1=document.getElementById("d1");
    dobj1.style.display="block";
    var dobj2=document.getElementById("d2");
    dobj2.style.display="none";
    }
    function stud(no,name,age,sex){
    this.no=no;
    this.name=name;
    this.age=age;
    this.sex=sex;
    }

    </script>
    </head>
    <body onload="creatTable()">
    <div id="d1">

    </div>
    <div id="d2" style="display: none">
    no :<input type="text" id="myNo"><br/>
    name:<input type="text" id="myName"><br>
    age :<input type="text" id="myAge"><br>
    sex :<input type="text" id="mySex"><br>
    <button type="button" value="更新" onclick="updateRow(this)">更新</button>
    <button type="button" value="返回" onclick="back()">返回</button>
    </div>
    </body>
    </html>

  • 相关阅读:
    jQuery学习教程(一):入门
    jQuery学习教程(八):事件
    jQuery学习教程(五):选择器综合实例
    jQuery学习教程(六):属性操作与CSS操作
    jQuery学习教程(四):使用jQuery操作DOM
    jQuery学习教程(七):val()与节点操作
    jQuery学习教程(二):选择器1
    const的使用
    ASP.NET 页面间传值的方法
    .net中接口与基类
  • 原文地址:https://www.cnblogs.com/suxiaojia/p/3863840.html
Copyright © 2011-2022 走看看