首页 > 代码库 > javascript表格的添加和删除
javascript表格的添加和删除
?
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> <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" > 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=http://www.mamicode.com/ "添加" /> <input type= "button" id= "btnDel" value=http://www.mamicode.com/ "删除" /> <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> |
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。