js增加删除table信息

举报
小傅哥 发表于 2021/04/23 01:24:08 2021/04/23
【摘要】 <html> <head> <title>作业</title><script language="javascript" type="text/javascript"> window.onload = function(){ var etable = "<table border = '1' id='myTable'>"+ "<tr align = 'center'> <t...

  
  1. <html>
  2. <head>
  3. <title>作业</title>
  4. <script language="javascript" type="text/javascript">
  5. window.onload = function(){
  6. var etable = "<table border = '1' id='myTable'>"+
  7. "<tr align = 'center'> <td>姓名</td> <td>性别</td> <td>年龄</td> <td>爱好</td> <td>操作</td></tr>"+
  8. "<tr> <td>付政委</td> <td>男</td> <td>24</td> <td>编程、看电影</td> "+
  9. "<td><span οnclick='delStu(this)' style='cursor:pointer; color=blue'>删除</span></td></tr>"+
  10. "</table><hr/>"+
  11. "增加新用户<br/>"+
  12. "姓名:<input type='text' id='stuName'/><br/>"+
  13. "性别:<lable><input type='radio' id='stuSex' name='stuSex' value='男' checked = 'checked'/>男</lable>"+
  14. " <lable><input type='radio' id='stuSex' name='stuSex' value='女'/>女</lable><br/>"+
  15. "年龄:<select id='stuAge'>"+
  16. " </select><br/>"+
  17. "爱好:<label><input type='checkbox' value='野游' name='stuhobby' id='stuhobby'>野游</label> "+
  18. " <label><input type='checkbox' value='逛街' name='stuhobb' id='stuhobby'>逛街</label>"+
  19. " <label><input type='checkbox' value='交友' name='stuhobby' id='stuhobby'>交友</label>"+
  20. " <label><input type='checkbox' value='其他' name='stuhobby' id='stuhobby'>其他</label><hr/>"+
  21. "<input type='button' value='新增用户' id='addStu'/>"
  22. ;
  23. document.body.innerHTML = etable;
  24. /*向年龄中插入元素*/
  25. for(var i = 10; i<=100; i++){
  26. var eOption = document.createElement("option");
  27. eOption.value = i;
  28. eOption.text = i+"岁";
  29. $("stuAge").add(eOption);
  30. }
  31. /*提取用户信息,并增加到列表*/
  32. $("addStu").onclick = function(){
  33. /*提取用户名*/
  34. var stu_name = $("stuName").value;
  35. /*提取性别*/
  36. var stu_sexs = document.getElementsByName("stuSex"),stu_sex = "";
  37. for(var sid = 0,slen= stu_sexs.length; sid < slen; sid ++){
  38. if(stu_sexs[sid].checked){
  39. stu_sex = stu_sexs[sid].value;
  40. }
  41. }
  42. /*获得年龄*/
  43. var stu_age = $("stuAge");
  44. stu_age = stu_age.options[stu_age.selectedIndex].value;
  45. /*获得爱好*/
  46. var stu_hobbys = document.getElementsByName("stuhobby"),stu_hobby="";
  47. for(var shid = 0,shlen = stu_hobbys.length; shid < shlen; shid ++){
  48. if(stu_hobbys[shid].checked){
  49. stu_hobby += stu_hobbys[shid].value +"、";
  50. }
  51. }
  52. /*把信息写入表格*/
  53. var trow = $("myTable").insertRow($("myTable").rows.length);
  54. var tcell_0 = trow.insertCell(0);
  55. var tcell_1 = trow.insertCell(1);
  56. var tcell_2 = trow.insertCell(2);
  57. var tcell_3 = trow.insertCell(3);
  58. var tcell_4 = trow.insertCell(4);
  59. tcell_0.innerHTML = stu_name;
  60. tcell_1.innerHTML = stu_sex;
  61. tcell_2.innerHTML = stu_age;
  62. tcell_3.innerHTML = stu_hobby;
  63. tcell_4.innerHTML = "<span οnclick='delStu(this)' style='cursor:pointer; color=blue'>删除</span>";
  64. }
  65. }
  66. /*删除控制*/
  67. function delStu(obj){
  68. /*删除提示框*/
  69. var pddel = window.confirm("确定删除?");
  70. if(pddel){
  71. var tr=obj.parentNode.parentNode;
  72. var tbody = tr.parentNode;
  73. tbody.removeChild(tr);
  74. }
  75. }
  76. /*刀了符号,用于调用获得dom*/
  77. function $(eid){
  78. return document.getElementById(eid);
  79. }
  80. </script>
  81. </head>
  82. <body>
  83. </body>
  84. </html>

文章来源: bugstack.blog.csdn.net,作者:小傅哥,版权归原作者所有,如需转载,请联系作者。

原文链接:bugstack.blog.csdn.net/article/details/7892712

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。