经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
jQuery-对列表的操作
来源:cnblogs  作者:Tynam.Yang  时间:2019/6/20 8:51:17  对本文有异议

主要是通过对dom元素的增加和删除实现对数据增加和删除

  1. 1 <!DOCTYPE html>
  2. 2 <html lang="en">
  3. 3
  4. 4 <head>
  5. 5 <meta charset="UTF-8">
  6. 6 <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. 7 <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. 8 <title>Document</title>
  9. 9 <script src="jquery-3.4.1.js"></script>
  10. 10 <style type="text/css">
  11. 11 * {
  12. 12 margin: 0;
  13. 13 padding: 0;
  14. 14 }
  15. 15
  16. 16 a {
  17. 17 text-decoration: none;
  18. 18 }
  19. 19
  20. 20 input {
  21. 21 outline: none;
  22. 22 font-size: 16px;
  23. 23 box-sizing: border-box;
  24. 24 border: aliceblue solid .5px;
  25. 25 }
  26. 26
  27. 27 body {
  28. 28 margin: 30px auto;
  29. 29 width: 510px;
  30. 30 }
  31. 31
  32. 32 .btn {
  33. 33 width: 80px;
  34. 34 height: 30px;
  35. 35 position: relative;
  36. 36 left: 380px;
  37. 37 margin-bottom: 10px;
  38. 38 }
  39. 39
  40. 40 .btn button {
  41. 41 width: 80px;
  42. 42 height: 30px;
  43. 43 background: #3ca7ed;
  44. 44 color: aliceblue;
  45. 45 font-size: 16px;
  46. 46 outline: none;
  47. 47 border-style: none;
  48. 48 }
  49. 49
  50. 50 .btn button:hover {
  51. 51 background: #0a84d4;
  52. 52 }
  53. 53
  54. 54 table {
  55. 55 border: solid aliceblue 1px;
  56. 56 border-collapse: collapse;
  57. 57 }
  58. 58
  59. 59 .code,
  60. 60 .name,
  61. 61 .sex,
  62. 62 .grader,
  63. 63 .del a,
  64. 64 input {
  65. 65 width: 100px;
  66. 66 height: 30px;
  67. 67 line-height: 30px;
  68. 68 padding-left: 5px;
  69. 69 }
  70. 70
  71. 71 tbody tr:nth-child(even) {
  72. 72 background: aliceblue;
  73. 73 }
  74. 74
  75. 75 .header {
  76. 76 background: #3ca7ed;
  77. 77 color: aliceblue;
  78. 78 }
  79. 79
  80. 80 .del a {
  81. 81 text-align: right;
  82. 82 padding-right: 5px;
  83. 83 color: #3ca7ed;
  84. 84 }
  85. 85
  86. 86 .opr a {
  87. 87 font-size: 16px;
  88. 88 color: #3ca7ed;
  89. 89 margin-left: 10px;
  90. 90 }
  91. 91 </style>
  92. 92
  93. 93 <style>
  94. 94 /* 删除/添加 */
  95. 95 .dialog,
  96. 96 .add-dialog {
  97. 97 display: none;
  98. 98 margin: 0 auto;
  99. 99 width: 400px;
  100. 100 height: 150px;
  101. 101 position: absolute;
  102. 102 top: 150px;
  103. 103 background: aliceblue;
  104. 104 text-align: center;
  105. 105 line-height: 65px;
  106. 106 font-size: 18px;
  107. 107 }
  108. 108
  109. 109 .add-dialog {
  110. 110 height: 270px;
  111. 111 }
  112. 112
  113. 113 .add-dialog div {
  114. 114 height: 40px;
  115. 115 }
  116. 116
  117. 117 .add-dialog div input {
  118. 118 width: 200px;
  119. 119 padding: 10px;
  120. 120 margin: 10px;
  121. 121 }
  122. 122
  123. 123 .dialog .header,
  124. 124 .add-dialog .header {
  125. 125 background: #3ca7ed;
  126. 126 color: aliceblue;
  127. 127 height: 35px;
  128. 128 text-align: left;
  129. 129 padding-left: 10px;
  130. 130 line-height: 35px;
  131. 131 }
  132. 132
  133. 133 .dialog button,
  134. 134 .add-dialog button {
  135. 135 display: inline-block;
  136. 136 width: 50px;
  137. 137 height: 30px;
  138. 138 background: #3ca7ed;
  139. 139 outline: none;
  140. 140 color: #f9f9f9;
  141. 141 border-style: none;
  142. 142 position: absolute;
  143. 143 bottom: 10px;
  144. 144 right: 10px;
  145. 145 }
  146. 146
  147. 147 .dialog button:hover,
  148. 148 .add-dialog button:hover {
  149. 149 background: #3ca7ed;
  150. 150 ;
  151. 151 }
  152. 152
  153. 153 #confirm {
  154. 154 right: 75px;
  155. 155 }
  156. 156 </style>
  157. 157
  158. 158 <script>
  159. 159 $(function () {
  160. 160 // 删除
  161. 161 function del_tr() {
  162. 162 var $tr = $(this).parent();
  163. 163 var code = $tr.children(':first').html();
  164. 164 $('.dialog').show();
  165. 165 $('.dialog .text').html("确定要删除[" + code + "]吗?");
  166. 166
  167. 167 $(".dialog #confirm").click(function () {
  168. 168 $($tr).remove();
  169. 169 $('.dialog').hide();
  170. 170 });
  171. 171
  172. 172 $(".dialog #concel").click(function () {
  173. 173 $('.dialog').hide();
  174. 174 });
  175. 175 };
  176. 176
  177. 177 $('.del').click(del_tr)
  178. 178
  179. 179 //添加
  180. 180 $('#student-add').click(function () {
  181. 181
  182. 182 $('.add-dialog').show();
  183. 183
  184. 184 $('.add-dialog #confirm').unbind('click'); //事件解绑
  185. 185 $('.add-dialog #confirm').click(function () {
  186. 186 var $code = $('.add-dialog .code');
  187. 187 var $name = $('.add-dialog .name');
  188. 188 var $sex = $('.add-dialog .sex');
  189. 189 var $grader = $('.add-dialog .grader');
  190. 190 var del = "<td class='del'><a href='#'>删除</a></td>";
  191. 191
  192. 192 var code = $code.val();
  193. 193 var name = $name.val();
  194. 194 var sex = $sex.val();
  195. 195 var grader = $grader.val();
  196. 196
  197. 197 $('<tr></tr>')
  198. 198 .append('<td>' + code + '</td>')
  199. 199 .append('<td>' + name + '</td>')
  200. 200 .append('<td>' + sex + '</td>')
  201. 201 .append('<td>' + grader + '</td>')
  202. 202 .append(del)
  203. 203 .appendTo('tbody')
  204. 204 .find('.del')
  205. 205 .click(del_tr)
  206. 206 // 输入框清空
  207. 207 $code = $('.add-dialog .code').val('');
  208. 208 $name = $('.add-dialog .name').val('');
  209. 209 $sex = $('.add-dialog .sex').val('');
  210. 210 $grader = $('.add-dialog .grader').val('');
  211. 211
  212. 212 $('.add-dialog').hide();
  213. 213
  214. 214 });
  215. 215
  216. 216 // 添加中取消按钮
  217. 217 $(".add-dialog #concel").click(function () {
  218. 218 $('.add-dialog').hide();
  219. 219
  220. 220 // 输入框清空
  221. 221 $code = $('.add-dialog .code').val('');
  222. 222 $name = $('.add-dialog .name').val('');
  223. 223 $sex = $('.add-dialog .sex').val('');
  224. 224 $grader = $('.add-dialog .grader').val('');
  225. 225 });
  226. 226 });
  227. 227 });
  228. 228 </script>
  229. 229 </head>
  230. 230
  231. 231 <body>
  232. 232 <div class="btn">
  233. 233 <button id="student-add">添加</button>
  234. 234 </div>
  235. 235 <table>
  236. 236 <tr class="header">
  237. 237 <td class="code">学号</td>
  238. 238 <td class="name">姓名</td>
  239. 239 <td class="sex">性别</td>
  240. 240 <td class="grader">年级</td>
  241. 241 <td class="del"><a href="#"></a></td>
  242. 242 </tr>
  243. 243 <tr>
  244. 244 <td class="code">30001</td>
  245. 245 <td class="name">段瑞琦</td>
  246. 246 <td class="sex"></td>
  247. 247 <td class="grader">三年级二班</td>
  248. 248 <td class="del"><a href="#">删除</a></td>
  249. 249 </tr>
  250. 250 <tr>
  251. 251 <td class="code">40002</td>
  252. 252 <td class="name">韩子萱</td>
  253. 253 <td class="sex"></td>
  254. 254 <td class="grader">四年级二班</td>
  255. 255 <td class="del"><a href="#">删除</a></td>
  256. 256 </tr>
  257. 257 <tr>
  258. 258 <td class="code">20101</td>
  259. 259 <td class="name">严寒</td>
  260. 260 <td class="sex"></td>
  261. 261 <td class="grader">二年级一班</td>
  262. 262 <td class="del"><a href="#">删除</a></td>
  263. 263 </tr>
  264. 264 <tr>
  265. 265 <td class="code">60012</td>
  266. 266 <td class="name">钱小龙</td>
  267. 267 <td class="sex"></td>
  268. 268 <td class="grader">六年级六班</td>
  269. 269 <td class="del"><a href="#">删除</a></td>
  270. 270 </tr>
  271. 271 </table>
  272. 272 <div class="dialog">
  273. 273 <div class="header">删除</div>
  274. 274 <div class="text"></div>
  275. 275 <button id="confirm">确定</button>
  276. 276 <button id="concel">取消</button>
  277. 277 </div>
  278. 278 <div class="add-dialog">
  279. 279 <div class="header">添加</div>
  280. 280 <div>学号<input type="text" class="code"></div>
  281. 281 <div>姓名<input type="text" class="name"></div>
  282. 282 <div>性别<input type="text" class="sex"></div>
  283. 283 <div>年级<input type="text" class="grader"></div>
  284. 284 <button id="confirm">确定</button>
  285. 285 <button id="concel">取消</button>
  286. 286 </div>
  287. 287 </body>
  288. 288
  289. 289 </html>

 

结果

原文链接:http://www.cnblogs.com/tynam/p/11055370.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号