首页 > 语言 > JavaScript > 正文

javascript实现信息增删改查的方法

2024-05-06 16:23:41
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了javascript实现信息增删改查的方法,实例分析了javascript操作页面元素实现针对页面信息的增删改查功能,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了javascript实现信息增删改查的方法。分享给大家供大家参考。具体实现方法如下:

 

 
  1. <body> 
  2. <div align="center"> 
  3. <h1>显示所有的用户界面</h1> 
  4. <div style="border: 1px red solid; margin-bottom: 100px; padding: 10px 10%;"> 
  5. <table border="1px" cellpadding="0" cellspacing="0" id="tusers"> 
  6. <thead> 
  7. <tr><th><input type="checkbox" name="chbk" id="chbk1" onclick="selectAll()"/></th> 
  8. <th>名称</th> 
  9. <th>性别</th> 
  10. <th>邮箱</th> 
  11. <th>出生日期</th> 
  12. <th>操作</th> 
  13. </tr> 
  14. </thead> 
  15. <tbody id="users"> 
  16. </tbody> 
  17. </table> 
  18. <div id="pages"></div>  
  19. </div>  
  20. <div style="border: 1px blue solid;"> 
  21. <form action=""> 
  22. <table id="divs"> 
  23. <tbody id="addUsers"> 
  24. <tr> 
  25. <td>用户名:</td> 
  26. <td><input type="text" name="name" id="name"/></td> 
  27. </tr> 
  28. <tr> 
  29. <td>性别:</td> 
  30. <td><select id="sex"> 
  31. <option value="男">男</option> 
  32. <option value="女">女</option> 
  33. </select> 
  34. </td> 
  35. </tr> 
  36. <tr> 
  37. <td>邮箱</td> 
  38. <td><input type="text" name="email" id="email"/></td> 
  39. </tr> 
  40. <tr> 
  41. <td>出生日期:</td> 
  42. <td> 
  43. <input type="text" id="bir" name="bir"/> 
  44. <input type=button value="添加日期" onclick="showCalender(this,document.all.bir)"/> 
  45. </td> 
  46. </tr> 
  47. <tr id="addu"> 
  48. <td colspan="2"><input type="button" value="添加" onclick="addUser()" id="add"/></td> 
  49. </tr> 
  50. <tr id="addu1"> 
  51. <td colspan="2"><input type="button" value="修改" id="upduser" /></td> 
  52. </tr> 
  53. </tbody> 
  54. </table> 
  55. </form>  
  56. </div> 
  57. </div> 
  58. </body> 
  59. <script> 
  60. window.onload = function(){ 
  61. alert("onload"); 
  62. document.getElementById("addu1").style.display = "none"; 
  63. } 
  64. function selectAll(){ 
  65. var users = document.getElementById("users"); 
  66. var ips = users.getElementsByTagName("input"); 
  67. var chbk = document.getElementById("chbk1"); 
  68. for(var i=0;i<ips.length;i++){ 
  69. ips[i].setAttribute("checked",chbk.getAttribute("checked")); 
  70. } 
  71. } 
  72. function addUser(){ 
  73. alert("add"); 
  74. var name = document.getElementById("name").Value; 
  75. var sex = document.getElementById("sex").Value; 
  76. var email = document.getElementById("email").Value; 
  77. var bir = document.getElementById("bir").Value; 
  78. var tusers = document.getElementById("tusers").Value; 
  79. var tr1 = document.createElement("tr"); 
  80. var cbk = document.createElement("td"); 
  81. var tname = document.createElement("td"); 
  82. var tsex = document.createElement("td"); 
  83. var temail = document.createElement("td"); 
  84. var tbir = document.createElement("td"); 
  85. var toper = document.createElement("td"); 
  86. var cbk1 = document.createElement("input"); 
  87. cbk1.setAttribute("type","checkbox"); 
  88. cbk1.setAttribute("name","chbk"); 
  89. cbk.appendChild(cbk1); 
  90. tname.appendChild(document.createTextNode(name)); 
  91. tsex.appendChild(document.createTextNode(sex)); 
  92. temail.appendChild(document.createTextNode(email)); 
  93. tbir.appendChild(document.createTextNode(bir)); 
  94. var adelete = document.createElement("a"); 
  95. var aupdate = document.createElement("a"); 
  96. adelete.setAttribute("href","#"); 
  97. aupdate.setAttribute("href","#"); 
  98. adelete.appendChild(document.createTextNode("删除|")); 
  99. aupdate.appendChild(document.createTextNode("修改")); 
  100. toper.appendChild(adelete); 
  101. toper.appendChild(aupdate); 
  102. tr1.appendChild(cbk); 
  103. tr1.appendChild(tname); 
  104. tr1.appendChild(tsex); 
  105. tr1.appendChild(temail); 
  106. tr1.appendChild(tbir); 
  107. tr1.appendChild(toper); 
  108. var users = document.getElementById("users"); 
  109. users.appendChild(tr1); 
  110. tusers.appendChild(users); 
  111. adelete.onclick = function(){ 
  112. users.removeChild(adelete.parentNode.parentNode); 
  113. } 
  114. aupdate.onclick function(){ 
  115. document.getElementById(addu).style.display = "none"; 
  116. document.getElementById(addu1).style.display = "block"; 
  117. var utr = aupdate.parentNode.parentNode; 
  118. var utrs = utr.childNodes; 
  119. document.getElementById("name").value = utrs[1].innerHTML; 
  120. document.getElementById("sex").value = utrs[2].innerHTML; 
  121. document.getElementById("email").value = utrs[3].innerHTML; 
  122. document.getElementById("bir").value = utrs[4].innerHTML; 
  123. var upUser = document.getElementById("upduser"); 
  124. upUser.onclick = function(){ 
  125. utr.childNodes[1].innerHTML = document.getElementById("name").value; 
  126. utr.childNodes[2].innerHTML = document.getElementById("sex").value; 
  127. utr.childNodes[3].innerHTML = document.getElementById("email").value; 
  128. utr.childNodes[4].innerHTML = document.getElementById("bir").value; 
  129. document.getElementById("addu1").style.display = "none"; 
  130. document.getElementById("addu").style.display = "block"; 
  131. } 
  132. } 
  133. testPage() 
  134. } 
  135. var indexPage = document.createElement("a"); 
  136. var upPage = document.createElement("a"); 
  137. var downPage = document.createElement("a"); 
  138. var endPage = document.createElement("a"); 
  139. var nowpage = 1; 
  140. function testPage(){ 
  141. var tbodyUsers = document.getElementById("users"); 
  142. var trUsers = document.getElementById("tr"); 
  143. var countRecord = trUsers.length; 
  144. var PAGESIZE = 2; 
  145. var countPage = (countRecord%PAGESIZE ==0?countRecord/PAGESIZE:Math.ceil(countRecord/PAGESIZE)); 
  146. var pages=document.getElementById("pages"); 
  147. if(!pages.hasChildNodes()){ 
  148. getPages(nowpage); 
  149. } 
  150. index.onclik=function(){ 
  151. noepage=1; 
  152. indexPageInfo(countRecord,trUsers); 
  153. } 
  154. upPage.onclick=function(){ 
  155. if(nowpage-1>1){ 
  156. nowpage-=1; 
  157. }else{ 
  158. nowpage=1; 
  159. indexPageInfo(countRecord,trUsers); 
  160. } 
  161. var startindex =(nowpage-1)*PAGESIZE; 
  162. var endindex=startindex+PAGESIZE; 
  163. PageInfo(startindex,endindex,countRecord,trUsers); 
  164. } 
  165. downPage.onclick=function(){ 
  166. if(nowpage+1>=countPage){ 
  167. nowpage=countPage; 
  168. }else{ 
  169. nowpage=+1; 
  170. } 
  171. var startindex =(nowpage-1)*PAGESIZE; 
  172. var endindex=startindex+PAGESIZE; 
  173. PageInfo(startindex,endindex,countRecord,trUsers); 
  174. } 
  175. endPage.onclick=function(){ 
  176. if(nowpage>1){ 
  177. var startindex =(nowpage-1)*PAGESIZE; 
  178. for(var i=0;i<countRecord;i++){ 
  179. if(i<startindex){ 
  180. trUsers[i].style.display="none"; 
  181. }else{ 
  182. trUsers[i].style.display="block"; 
  183. } 
  184. } 
  185. }else{ 
  186. indexPageInfo(countRecord,trUsers); 
  187. } 
  188. } 
  189. } 
  190. function indexPageInfo(countRecord, trUsers) { 
  191. if (countRecord <= 2) { 
  192. for ( var i = 0; i < PAGESIZE; i++) { 
  193. trUsers[i].style.display = "block"; 
  194. } 
  195. } else { 
  196. for ( var i = 2; i < countRecord; i++) { 
  197. trUsers[i].style.display = "none"; 
  198. } 
  199. } 
  200. } 
  201. function PageInfo(startindex, endindex, countRecord, trUsers) { 
  202. for ( var i = 0; i < countRecord; i++) { 
  203. if (i >= startindex && i < endindex) { 
  204. trUsers[i].style.display = "block"; 
  205. } else { 
  206. trUsers[i].style.display = "none"; 
  207. } 
  208. } 
  209. } 
  210. function getPages(numpage) { 
  211. indexPage.appendChild(document.createTextNode("首页")); 
  212. indexPage.setAttribute("href", "#"); 
  213. upPage.appendChild(document.createTextNode("上一页")); 
  214. upPage.setAttribute("href", "#"); 
  215. downPage.appendChild(document.createTextNode("下一页")); 
  216. downPage.setAttribute("href", "#"); 
  217. endPage.appendChild(document.createTextNode("末页")); 
  218. endPage.setAttribute("href", "#"); 
  219. var pages = document.getElementById("pages"); 
  220. pages.appendChild(indexPage); 
  221. pages.appendChild(upPage); 
  222. pages.appendChild(downPage); 
  223. pages.appendChild(endPage); 
  224. } 
  225. </script> 

希望本文所述对大家的javascript程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选