dom 动态生产表格

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
    #form1 {
        width: 480px;
        border: 1px solid #eee;
        border-radius: 5px;
        margin: 30px auto;
        padding: 10px;
        line-height: 30px;
        position: relative;
    }
    #tab1 {
        width: 500px;
        margin: 30px auto;
        border-collapse: collapse;
    }
    th, td {
        border: 1px solid #000;
        padding: 5px;
    }
    tr {
        cursor: pointer;
    }
    tbody tr td:first-child {
        text-align: center;
    }
    input[type="checkbox"] {
        width: 15px;
        height: 15px;
    }
    input[type="button"] {
        position: absolute;
        right: 10px;
        bottom: 10px;
    }
    #div1 {
        position: relative;
        width: 480px;
        padding: 10px;
        margin: 0 auto;
    }
</style>
<script type="text/javascript">
    function getChecked(form,name)
    {
        var arr = [];
        for (var i = 0; i < form[name].length; i++)
        {
            if(form[name][i].checked)
            {
                arr.push(form[name][i].value)
            }
        }
        if(form[name][0].type == ‘radio‘)
        {
            return arr[0];
        }
        if(form[name][0].type == ‘checkbox‘)
        {
            return arr;
        }
    };
</script>
<script type="text/javascript">
window.onload = function ()
{
    var data = [
    {‘name‘: ‘赵一‘, ‘sex‘: ‘男‘, ‘age‘: ‘35‘},
    {‘name‘: ‘钱二‘, ‘sex‘: ‘女‘, ‘age‘: ‘28‘},
    {‘name‘: ‘孙三‘, ‘sex‘: ‘男‘, ‘age‘: ‘15‘},
    {‘name‘: ‘李四‘, ‘sex‘: ‘男‘, ‘age‘: ‘48‘},
    {‘name‘: ‘周五‘, ‘sex‘: ‘男‘, ‘age‘: ‘36‘},
    {‘name‘: ‘武六‘, ‘sex‘: ‘女‘, ‘age‘: ‘49‘},
    {‘name‘: ‘郑七‘, ‘sex‘: ‘女‘, ‘age‘: ‘75‘},
    {‘name‘: ‘王九‘, ‘sex‘: ‘男‘, ‘age‘: ‘17‘},
    ];

    var otab = document.getElementById(‘tab1‘);
    var obody = otab.tBodies[0];
    var oform = document.getElementById(‘form1‘);
    var oall = document.getElementById(‘checkAll‘);
    var odelet = document.getElementById(‘delete‘);

    for( var i = 0; i < data.length; i++)
    {
        var otr = document.createElement(‘tr‘);

        var otd = document.createElement(‘td‘);
        otd.innerHTML = ‘<input type="checkbox">‘
        otr.appendChild(otd);

        var otd = document.createElement(‘td‘);
        otd.innerHTML = data[i].name;
        otr.appendChild(otd);

        var otd = document.createElement(‘td‘);
        otd.innerHTML = data[i].sex;
        otr.appendChild(otd);

        var otd = document.createElement(‘td‘);
        otd.innerHTML = data[i].age;
        otr.appendChild(otd);

        obody.appendChild(otr);

        colorline();
    }

    checkbox();
    oform.add.onclick = function ()
    {
        arr = [];
        arr[0] = oform.name.value;
        arr[1] = getChecked(oform,‘sex‘);
        arr[2] = oform.age.value;

        for(var i = 0; i < arr.length; i++ )
        {
            var otr = document.createElement(‘tr‘);
            var otd = document.createElement(‘td‘);
            otd.innerHTML = ‘<input type="checkbox">‘
            otr.appendChild(otd);

            for(var i = 0; i < arr.length; i++)
            {
                var otd = document.createElement(‘td‘);
                otd.innerHTML = arr[i];
                otr.appendChild(otd);
            }
        }
        obody.appendChild(otr);
        oform.reset();
        colorline();
        checkbox();
    };

    function colorline()
    {
        for( var i = 0; i < obody.rows.length; i++ )
        {
            if(i%2)
            {
                obody.rows[i].style.background = ‘#fff‘;
            }
            else
            {
                obody.rows[i].style.background = ‘pink‘;
            }

        }
    };

    function checkbox()
    {
        for(var i = 0; i < obody.rows.length; i++ )
        {
            obody.rows[i].onclick = function ()
            {
                this.cells[0].children[0].checked = this.cells[0].children[0].checked == false?true:false;
                check();
            }

            obody.rows[i].cells[0].children[0].onclick = function ()
            {
                this.checked = this.checked == false ? true:false;
                check();
            }
        }
    }

    otab.tHead.onclick = function ()
    {
        oall.checked = oall.checked == false ? true: false;
        checkall();
    };

    oall.onclick = function ()
    {
        this.checked = this.checked == false ? true:false;
        checkall();
    }

    function checkall()
    {
        for( var i = 0; i < obody.rows.length; i++ )
        {
            obody.rows[i].cells[0].children[0].checked = oall.checked;
        }
    };

    function check()
    {
        for( var i = 0; i < obody.rows.length; i++)
        {
            if(!obody.rows[i].cells[0].children[0].checked)
            {
                oall.checked = false;
                return;
            }
        }
        oall.checked = true;
    }

    odelet.onclick = function ()
    {
        oall.checked = false;
        for(var i = 0; i < obody.rows.length; i++ )
        {
            if(obody.rows[i].cells[0].children[0].checked)
            {
                obody.removeChild(obody.rows[i])
                i--;
            }
        }
        colorline();
    }
}
</script>
</head>

<body>
    <form id="form1">
        请输入姓名:<input type="text" name="name"><br>
        请选择性别:<input type="radio" name="sex" value="男">男<input type="radio" name="sex" value="女">女<br>
        请输入年龄:<input type="text" name="age"><br>
        <input type="button" value="添加到表格" name="add">
    </form>
    <table id="tab1">
        <thead>
            <tr>
                <th width="20%"><input type="checkbox" id="checkAll"  id="checkAll"/> 全选</th>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
    <div id="div1">
        <input type="button" value="删除所选行" id="delete">
    </div>
</body>
</html>
时间: 2024-12-29 11:55:59

dom 动态生产表格的相关文章

10JavaScript DOM动态创建表格第二种方法(建议使用的方法)

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script type="text/javascript"> var dic = {

0166 DOM 之 节点操作: 删除节点,删除留言案例,复制(克隆)节点,动态生成表格案例,创建元素的三种方式,innerHTML和createElement效率对比

1.1.1 删除节点 node.removeChild(child) // 此处的node指 父节点 node.removeChild() 方法: 从 node节点中删除一个子节点,返回删除的节点. <button>删除</button> <ul> <li>熊大</li> <li>熊二</li> <li>光头强</li> </ul> <script> // 1.获取元素 va

ajax动态生成表格

<script> var data = [{ name: "传智播客", url: "http://www.itcast.cn", type: "IT最强培训机构" },{ name: "黑马程序员", url: "http://www.itheima.com", type: "大学生IT培训机构" },{ name: "传智前端学院", url: &qu

javascript生成表格增删改查 JavaScript动态改变表格单元格内容 动态生成表格 JS获取表格任意单元格 javascript如何动态删除表格某一行

jsp页面表格布局Html代码 <body > <center> <input type="text" value="111" id="mytext"> <table border="1" width="60%" id="mytable"> <tr> <td id="td1">第一行第一列<

javascript动态创建表格:新增、删除行和列

转载:http://www.cnblogs.com/pato/archive/2009/09/02/1559068.html 利用js来动态创建表格有两种格式,appendChild()和insertRow.insertCell().两种方式其实差不多,但第一种有可能在IE上有问题,所以推荐大家使用第二种了,直接说吧. 1.inserRow()和insertCell()函数 insertRow()函数可以带参数,形式如下: insertRow(index):index从0开始 这个函数将新行添加

动态添加表格

1.表格包含的HTML DOM对象 2.Table对象 3.TableRow对象 4.TableCell对象 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml&qu

html中动态创建表格

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>dtTable.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equ

Android动态生成表格

最近刚刚学习完Android的五大布局,现在我们进一步深入学习,尝试做一个动态生成表格功能的例子 样式布局代码如下: 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4

Javascript:DOM动态创建元素实例应用

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Dom:动态创建元素</title> </head> <body> <ul id="demo1"> </ul> <input type="text" id=&quo