码迷,mamicode.com
首页 > 编程语言 > 详细

javascript表格的添加和删除

时间:2014-06-10 12:23:00      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type="text/javascript">
        window.onload = function () {
            var oTab = document.getElementById("tb1");
 
            var oBtn = document.getElementById("btnAdd");
            var oName = document.getElementById("txtName");
            var oAge = document.getElementById("txtAge");
            oBtn.onclick = function () {
                var oTr = document.createElement("tr");
                //第一列
                var oTd = document.createElement("td");
                //alert(oTab.tBodies[0].rows.length + 1);
                oTd.innerText = oTab.tBodies[0].rows.length + 1;
                oTr.appendChild(oTd);
                //第二列
                var oTd = document.createElement("td");
                // alert(oName.value);
                oTd.innerText = oName.value;
                oTr.appendChild(oTd);
 
                //第三号
                var oTd = document.createElement("td");
                // alert(oAge.value);
                oTd.innerText = oAge.value;
                oTr.appendChild(oTd);
 
                //第三号
                var oTd = document.createElement("td");
                // alert(oAge.value);
                oTd.innerText = "<a href=‘javascript:void(0);‘>删除</a>";
                oTr.appendChild(oTd);
 
                oTd.getElementsByTagName(‘a‘)[0].onclick = function () {
                    oTab.removeChild(this.parentNode.parentNode);
                };
 
                oTab.tBodies[0].appendChild(oTr);
            }
        }
    </script>
</head>
<body>
    姓名:<input type="text" id="txtName" /> 
    年龄:<input type="text" id="txtAge" />
    <input type="button" id="btnAdd" value="添加" />
    <input type="button" id="btnDel" value="删除" />
    <table id="tb1" border="1" style="width: 500px;">
        <thead>
            <tr>
                <td>ID</td>
                <td>姓名</td>
                <td>年龄</td>
                <td></td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>Cupid</td>
                <td>66</td>
                <td></td>
            </tr>
            <tr>
                <td>2</td>
                <td>Fly</td>
                <td>45</td>
                <td></td>
            </tr>
            <tr>
                <td>3</td>
                <td>Sky</td>
                <td>23</td>
            </tr>
            <tr>
                <td>4</td>
                <td>Windy</td>
                <td>98</td>
            </tr>
            <tr>
                <td>5</td>
                <td>Snow</td>
                <td>09</td>
                <td></td>
            </tr>
        </tbody>
    </table>
</body>
</html>

  

javascript表格的添加和删除,布布扣,bubuko.com

javascript表格的添加和删除

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/alphafly/p/3778876.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!