js对table执行增-删-改

举报
小傅哥 发表于 2021/04/21 23:44:37 2021/04/21
【摘要】 <html> <head> <title>作业</title><script language="javascript" type="text/javascript"> /*页面加载完成*/ window.onload = function(){ var etable = "<table border = '1' id='myTable'>"+ "<tr align = '...

  
  1. <html>
  2. <head>
  3. <title>作业</title>
  4. <script language="javascript" type="text/javascript">
  5. /*页面加载完成*/
  6. window.onload = function(){
  7. var etable = "<table border = '1' id='myTable'>"+
  8. "<tr align = 'center'> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>爱好</th> <th>操作</th></tr>"+
  9. "<tr> <td οnclick='updStu(this)' name='付政委'>付政委</td>"+
  10. " <td οnclick='updStu(this)' name='男'>男</td>"+
  11. " <td οnclick='updStu(this)' name='24'>24</td>"+
  12. " <td οnclick='updStu(this)' name='编程、看电影'>编程、看电影</td> "+
  13. "<td><span οnclick='delStu(this)' style='cursor:pointer; color=red'>删除</span>"+
  14. "</td></tr>"+
  15. "</table><hr/>"+
  16. "增加新用户<br/>"+
  17. "姓名:<input type='text' id='stuName'/><br/>"+
  18. "性别:<lable><input type='radio' id='stuSex' name='stuSex' value='男' checked = 'checked'/>男</lable>"+
  19. " <lable><input type='radio' id='stuSex' name='stuSex' value='女'/>女</lable><br/>"+
  20. "年龄:<select id='stuAge'>"+
  21. " </select><br/>"+
  22. "爱好:<label><input type='checkbox' value='野游' name='stuhobby' id='stuhobby'>野游</label> "+
  23. " <label><input type='checkbox' value='逛街' name='stuhobb' id='stuhobby'>逛街</label>"+
  24. " <label><input type='checkbox' value='交友' name='stuhobby' id='stuhobby'>交友</label>"+
  25. " <label><input type='checkbox' value='其他' name='stuhobby' id='stuhobby'>其他</label><hr/>"+
  26. "<input type='button' value='新增用户' id='addStu'/>"
  27. ;
  28. document.body.innerHTML = etable;
  29. /*向年龄中插入元素*/
  30. for(var i = 10; i<=100; i++){
  31. var eOption = document.createElement("option");
  32. eOption.value = i;
  33. eOption.text = i+"岁";
  34. $("stuAge").add(eOption);
  35. }
  36. /*提取用户信息,并增加到列表*/
  37. $("addStu").onclick = function(){
  38. /*提取用户名*/
  39. var stu_name = $("stuName").value;
  40. /*提取性别*/
  41. var stu_sexs = document.getElementsByName("stuSex"),stu_sex = "";
  42. for(var sid = 0,slen= stu_sexs.length; sid < slen; sid ++){
  43. if(stu_sexs[sid].checked){
  44. stu_sex = stu_sexs[sid].value;
  45. }
  46. }
  47. /*获得年龄*/
  48. var stu_age = $("stuAge");
  49. stu_age = stu_age.options[stu_age.selectedIndex].value;
  50. /*获得爱好*/
  51. var stu_hobbys = document.getElementsByName("stuhobby"),stu_hobby="";
  52. for(var shid = 0,shlen = stu_hobbys.length; shid < shlen; shid ++){
  53. if(stu_hobbys[shid].checked){
  54. stu_hobby += stu_hobbys[shid].value +"、";
  55. }
  56. }
  57. /*把信息写入表格*/
  58. var trow = $("myTable").insertRow($("myTable").rows.length);
  59. //根据当前插入到的长度,改变表格背景色
  60. if(($("myTable").rows.length)%2==1){
  61. trow.style.backgroundColor="pink";
  62. }
  63. var tcell_0 = trow.insertCell(0);
  64. var tcell_1 = trow.insertCell(1);
  65. var tcell_2 = trow.insertCell(2);
  66. var tcell_3 = trow.insertCell(3);
  67. var tcell_4 = trow.insertCell(4);
  68. tcell_0.innerHTML = stu_name;
  69. tcell_1.innerHTML = stu_sex;
  70. tcell_2.innerHTML = stu_age;
  71. tcell_3.innerHTML = stu_hobby;
  72. tcell_4.innerHTML = "<span οnclick='delStu(this)' style='cursor:pointer; color=red'>删除</span>";
  73. /*添加事件*/
  74. tcell_0.name = stu_name;
  75. tcell_0.onclick = updateUser;
  76. tcell_1.name = stu_sex;
  77. tcell_1.onclick = updateUser;
  78. tcell_2.name = stu_age;
  79. tcell_2.onclick = updateUser;
  80. tcell_3.name = stu_hobby;
  81. tcell_3.onclick = updateUser;
  82. /*定义事件*/
  83. function updateUser(){
  84. var userName = "";
  85. userName = this.name;
  86. this.innerHTML = "<input type='text' id='updF'/>";
  87. $("updF").focus();
  88. $("updF").value = userName;
  89. $("updF").onmousedown = function(){
  90. if($("updF").value.length > 0){
  91. this.parentNode.innerHTML = $("updF").value;
  92. }else{
  93. this.parentNode.innerHTML = userName;
  94. }
  95. }
  96. }
  97. }
  98. }
  99. /*修改表格信息*/
  100. function updStu(obj){
  101. var userName = "";
  102. userName = obj.name;
  103. obj.innerHTML = "<input type='text' id='updF'/>";
  104. $("updF").focus();
  105. $("updF").value = userName;
  106. $("updF").onmousedown = function(){
  107. if($("updF").value.length > 0){
  108. obj.innerHTML = $("updF").value;
  109. }else{
  110. obj.innerHTML = userName;
  111. }
  112. }
  113. }
  114. /*删除控制*/
  115. function delStu(obj){
  116. /*删除提示框*/
  117. var pddel = window.confirm("确定删除?");
  118. if(pddel){
  119. var tr=obj.parentNode.parentNode;
  120. var tbody = tr.parentNode;
  121. tbody.removeChild(tr);
  122. }
  123. }
  124. /*刀了符号,用于调用获得dom*/
  125. function $(eid){
  126. return document.getElementById(eid);
  127. }
  128. </script>
  129. </head>
  130. <body>
  131. </body>
  132. </html>


 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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