首页 > 语言 > JavaScript > 正文

Js实现无刷新删除内容

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

本文给大家分享的是一段仿腾讯微博的无刷新删除特效的代码,非常的实用,有需要的小伙伴可以参考下。

Js实现无刷新删除内容

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <title>仿腾讯微博效果</title> 
  6. <style type="text/css"> 
  7. body,div,h2,h3,ul,li,p{margin:0;padding:0;} 
  8. a{text-decoration:none;} 
  9. a:hover{text-decoration:underline;} 
  10. ul{list-style-type:none;} 
  11. body{color:#333;background:#3c3a3b;font:12px/1.5 /5b8b/4f53;} 
  12. #msgBox{width:500px;background:#fff;border-radius:5px;margin:10px auto;padding-top:10px;} 
  13. #msgBox form h2{font-weight:400;font:400 18px/1.5 /5fae/8f6f/96c5/9ed1;} 
  14. #msgBox form{background:url() repeat-x 0 bottom;padding:0 20px 15px;} 
  15. #userName,#conBox{color:#777;border:1px solid #d0d0d0;border-radius:6px;background:#fff url(img/inputBG.png) repeat-x;padding:3px 5px;font:14px/1.5 arial;} 
  16. #userName.active,#conBox.active{border:1px solid #7abb2c;} 
  17. #userName{height:20px;} 
  18. #conBox{width:448px;resize:none;height:65px;overflow:auto;} 
  19. #msgBox form div{position:relative;color:#999;margin-top:10px;} 
  20. #msgBox img{border-radius:3px;} 
  21. #face{position:absolute;top:0;left:172px;} 
  22. #face img{width:30px;height:30px;cursor:pointer;margin-right:6px;opacity:0.5;filter:alpha(opacity=50);} 
  23. #face img.hover,#face img.current{width:28px;height:28px;border:1px solid #f60;opacity:1;filter:alpha(opacity=100);} 
  24. #sendBtn{border:0;width:112px;height:30px;cursor:pointer;margin-left:10px;background:url(img/btn.png) no-repeat;} 
  25. #sendBtn.hover{background-position:0 -30px;} 
  26. #msgBox form .maxNum{font:26px/30px Georgia, Tahoma, Arial;padding:0 5px;} 
  27. #msgBox .list{padding:10px;} 
  28. #msgBox .list h3{position:relative;height:33px;font-size:14px;font-weight:400;background:#e3eaec;border:1px solid #dee4e7;} 
  29. #msgBox .list h3 span{position:absolute;left:6px;top:6px;background:#fff;line-height:28px;display:inline-block;padding:0 15px;} 
  30. #msgBox .list ul{overflow:hidden;zoom:1;} 
  31. #msgBox .list ul li{float:left;clear:both;width:100%;border-bottom:1px dashed #d8d8d8;padding:10px 0;background:#fff;overflow:hidden;} 
  32. #msgBox .list ul li.hover{background:#f5f5f5;} 
  33. #msgBox .list .userPic{float:left;width:50px;height:50px;display:inline;margin-left:10px;border:1px solid #ccc;border-radius:3px;} 
  34. #msgBox .list .content{float:left;width:400px;font-size:14px;margin-left:10px;font-family:arial;word-wrap:break-word;} 
  35. #msgBox .list .userName{display:inline;padding-right:5px;} 
  36. #msgBox .list .userName a{color:#2b4a78;} 
  37. #msgBox .list .msgInfo{display:inline;word-wrap:break-word;} 
  38. #msgBox .list .times{color:#889db6;font:12px/18px arial;margin-top:5px;overflow:hidden;zoom:1;} 
  39. #msgBox .list .times span{float:left;} 
  40. #msgBox .list .times a{float:right;color:#889db6;display:none;} 
  41. .tr{overflow:hidden;zoom:1;} 
  42. .tr p{float:right;line-height:30px;} 
  43. .tr *{float:left;} 
  44. </style> 
  45. <script type="text/javascript"> 
  46. var get = { 
  47. byId: function(id) { 
  48. return typeof id === "string" ? document.getElementById(id) : id 
  49. }, 
  50. byClass: function(sClass, oParent) { 
  51. var aClass = []; 
  52. var reClass = new RegExp("(^| )" + sClass + "( |$)"); 
  53. var aElem = this.byTagName("*", oParent); 
  54. for (var i = 0; i < aElem.length; i++) reClass.test(aElem[i].className) && aClass.push(aElem[i]); 
  55. return aClass 
  56. }, 
  57. byTagName: function(elem, obj) { 
  58. return (obj || document).getElementsByTagName(elem) 
  59. } 
  60. }; 
  61. /*-------------------------- + 
  62. 事件绑定, 删除 
  63. +-------------------------- */ 
  64. var EventUtil = { 
  65. addHandler: function (oElement, sEvent, fnHandler) { 
  66. oElement.addEventListener ? oElement.addEventListener(sEvent, fnHandler, false) : (oElement["_" + sEvent + fnHandler] = fnHandler, oElement[sEvent + fnHandler] 
  67.  
  68.  
  69. = function () {oElement["_" + sEvent + fnHandler]()}, oElement.attachEvent("on" + sEvent, oElement[sEvent + fnHandler])) 
  70. }, 
  71. removeHandler: function (oElement, sEvent, fnHandler) { 
  72. oElement.removeEventListener ? oElement.removeEventListener(sEvent, fnHandler, false) : oElement.detachEvent("on" + sEvent, oElement[sEvent + fnHandler]) 
  73. }, 
  74. addLoadHandler: function (fnHandler) { 
  75. this.addHandler(window, "load", fnHandler) 
  76. } 
  77. }; 
  78. /*-------------------------- + 
  79. 设置css样式 
  80. 读取css样式 
  81. +-------------------------- */ 
  82. function css(obj, attr, value) 
  83. { 
  84. switch (arguments.length) 
  85. { 
  86. case 2: 
  87. if(typeof arguments[1] == "object") 
  88. {  
  89. for (var i in attr) i == "opacity" ? (obj.style["filter"] = "alpha(opacity=" + attr[i] + ")", obj.style[i] = attr[i] / 100) : obj.style[i] = attr[i]; 
  90. } 
  91. else 
  92. {  
  93. return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj, null)[attr] 
  94. } 
  95. break; 
  96. case 3: 
  97. attr == "opacity" ? (obj.style["filter"] = "alpha(opacity=" + value + ")", obj.style[attr] = value / 100) : obj.style[attr] = value; 
  98. break; 
  99. } 
  100. }; 
  101.  
  102.  
  103. EventUtil.addLoadHandler(function () 
  104. { 
  105. var oMsgBox = get.byId("msgBox"); 
  106. var oUserName = get.byId("userName"); 
  107. var oConBox = get.byId("conBox"); 
  108. var oSendBtn = get.byId("sendBtn"); 
  109. var oMaxNum = get.byClass("maxNum")[0]; 
  110. var oCountTxt = get.byClass("countTxt")[0]; 
  111. var oList = get.byClass("list")[0]; 
  112. var oUl = get.byTagName("ul", oList)[0]; 
  113. var aLi = get.byTagName("li", oList); 
  114. var aFtxt = get.byClass("f-text", oMsgBox); 
  115. var aImg = get.byTagName("img", get.byId("face")); 
  116. var bSend = false; 
  117. var timer = null; 
  118. var oTmp = ""; 
  119. var i = 0; 
  120. var maxNum = 140; 
  121.  
  122. //禁止表单提交 
  123. EventUtil.addHandler(get.byTagName("form", oMsgBox)[0], "submit", function () {return false}); 
  124.  
  125. //为广播按钮绑定发送事件 
  126. EventUtil.addHandler(oSendBtn, "click", fnSend); 
  127.  
  128. //为Ctrl+Enter快捷键绑定发送事件 
  129. EventUtil.addHandler(document, "keyup", function(event) 
  130. { 
  131. var event = event || window.event; 
  132. event.ctrlKey && event.keyCode == 13 && fnSend() 
  133. }); 
  134.  
  135. //发送广播函数 
  136. function fnSend () 
  137. { 
  138. var reg = /^/s*$/g; 
  139. if(reg.test(oUserName.value)) 
  140. { 
  141. alert("/u8bf7/u586b/u5199/u60a8/u7684/u59d3/u540d"); 
  142. oUserName.focus() 
  143. } 
  144. else if(!/^[u4e00-/u9fa5/w]{2,8}$/g.test(oUserName.value)) 
  145. { 
  146. alert("/u59d3/u540d/u75312-8/u4f4d/u5b57/u6bcd/u3001/u6570/u5b57/u3001/u4e0b/u5212/u7ebf/u3001/u6c49/u5b57/u7ec4/u6210/uff01"); 
  147. oUserName.focus() 
  148. } 
  149. else if(reg.test(oConBox.value)) 
  150. { 
  151. alert("/u968f/u4fbf/u8bf4/u70b9/u4ec0/u4e48/u5427/uff01"); 
  152. oConBox.focus() 
  153. } 
  154. else if(!bSend) 
  155. { 
  156. alert("/u4f60/u8f93/u5165/u7684/u5185/u5bb9/u5df2/u8d85/u51fa/u9650/u5236/uff0c/u8bf7/u68c0/u67e5/uff01"); 
  157. oConBox.focus() 
  158. } 
  159. else 
  160. { 
  161. var oLi = document.createElement("li"); 
  162. var oDate = new Date(); 
  163. oLi.innerHTML = "<div class=/"userPic/"><img src=/"" + get.byClass("current", get.byId("face"))[0].src + "/"></div>/ 
  164. <div class=/"content/">/ 
  165. <div class=/"userName/"><a href=/"javascript:;/">" + oUserName.value + "</a>:</div>/ 
  166. <div class=/"msgInfo/">" + oConBox.value.replace(/<[^>]*>|/ig, "") + "</div>/ 
  167. <div class=/"times/"><span>" + format(oDate.getMonth() + 1) + "/u6708" + format(oDate.getDate()) + 
  168.  
  169.  
  170. "/u65e5 " + format(oDate.getHours()) + ":" + format(oDate.getMinutes()) + "</span><a class=/"del/" href=/"javascript:;/">/u5220/u9664</a></div>/ 
  171. </div>"; 
  172. //插入元素 
  173. aLi.length ? oUl.insertBefore(oLi, aLi[0]) : oUl.appendChild(oLi); 
  174.  
  175. //重置表单 
  176. get.byTagName("form", oMsgBox)[0].reset(); 
  177. for (i = 0; i < aImg.length; i++) aImg[i].className = ""; 
  178. aImg[0].className = "current"; 
  179.  
  180. //将元素高度保存 
  181. var iHeight = oLi.clientHeight - parseFloat(css(oLi, "paddingTop")) - parseFloat(css(oLi, "paddingBottom")); 
  182. var alpah = count = 0; 
  183. css(oLi, {"opacity" : "0", "height" : "0"}); 
  184.  
  185. timer = setInterval(function () 
  186. { 
  187. css(oLi, {"display" : "block", "opacity" : "0", "height" : (count += 8) + "px"}); 
  188. if (count > iHeight) 
  189. { 
  190. clearInterval(timer); 
  191. css(oLi, "height", iHeight + "px"); 
  192. timer = setInterval(function () 
  193. { 
  194. css(oLi, "opacity", (alpah += 10)); 
  195. alpah > 100 && (clearInterval(timer), css(oLi, "opacity", 100)) 
  196. },30) 
  197. } 
  198. },30); 
  199. //调用鼠标划过/离开样式 
  200. liHover(); 
  201. //调用删除函数 
  202. delLi() 
  203. } 
  204. }; 
  205.  
  206. //事件绑定, 判断字符输入 
  207. EventUtil.addHandler(oConBox, "keyup", confine); 
  208.  
  209. EventUtil.addHandler(oConBox, "focus", confine); 
  210. EventUtil.addHandler(oConBox, "change", confine); 
  211.  
  212. //输入字符限制 
  213. function confine () 
  214. { 
  215. var iLen = 0;  
  216. for (i = 0; i < oConBox.value.length; i++) iLen += oConBox.value.charAt(i).charCodeAt() > 255 ? 1 : 0.5; 
  217. oMaxNum.innerHTML = Math.abs(maxNum - Math.floor(iLen)); 
  218.  
  219. maxNum - Math.floor(iLen) >= 0 ? (css(oMaxNum, "color", ""), oCountTxt.innerHTML = "/u8fd8/u80fd/u8f93/u5165", bSend = true) : (css(oMaxNum, "color", "#f60"), 
  220.  
  221.  
  222. oCountTxt.innerHTML = "/u5df2/u8d85/u51fa", bSend = false) 
  223. } 
  224. //加载即调用 
  225. confine(); 
  226.  
  227. //广播按钮鼠标划过样式 
  228. EventUtil.addHandler(oSendBtn, "mouseover", function () {this.className = "hover"}); 
  229.  
  230.  
  231. //广播按钮鼠标离开样式 
  232. EventUtil.addHandler(oSendBtn, "mouseout", function () {this.className = ""}); 
  233.  
  234. //li鼠标划过/离开处理函数 
  235. function liHover() 
  236. { 
  237. for (i = 0; i < aLi.length; i++) 
  238. { 
  239. //li鼠标划过样式 
  240. EventUtil.addHandler(aLi[i], "mouseover", function (event) 
  241. { 
  242. this.className = "hover"; 
  243. oTmp = get.byClass("times", this)[0]; 
  244. var aA = get.byTagName("a", oTmp); 
  245. if (!aA.length) 
  246. { 
  247. var oA = document.createElement("a"); 
  248.  
  249. oA.innerHTML = "删除"; 
  250. oA.className = "del"; 
  251. oA.href = "javascript:;"; 
  252. oTmp.appendChild(oA) 
  253. } 
  254. else 
  255. { 
  256. aA[0].style.display = "block"; 
  257. } 
  258. }); 
  259.  
  260.  
  261. //li鼠标离开样式 
  262. EventUtil.addHandler(aLi[i], "mouseout", function () 
  263. { 
  264. this.className = ""; 
  265. var oA = get.byTagName("a", get.byClass("times", this)[0])[0]; 
  266. oA.style.display = "none" 
  267.  
  268. }) 
  269. } 
  270. } 
  271. liHover(); 
  272.  
  273. //删除功能 
  274. function delLi() 
  275. { 
  276. var aA = get.byClass("del", oUl); 
  277.  
  278. for (i = 0; i < aA.length; i++) 
  279. { 
  280. aA[i].onclick = function () 
  281. { 
  282. var oParent = this.parentNode.parentNode.parentNode; 
  283. var alpha = 100; 
  284. var iHeight = oParent.offsetHeight; 
  285. timer = setInterval(function () 
  286. { 
  287. css(oParent, "opacity", (alpha -= 10)); 
  288. if (alpha < 0) 
  289. { 
  290. clearInterval(timer); 
  291.  
  292. timer = setInterval(function () 
  293. { 
  294. iHeight -= 10; 
  295. iHeight < 0 && (iHeight = 0); 
  296. css(oParent, "height", iHeight + "px"); 
  297. iHeight == 0 && (clearInterval(timer), oUl.removeChild(oParent)) 
  298. },30) 
  299. }  
  300. },30);  
  301. this.onclick = null 
  302.  
  303. }  
  304. } 
  305. } 
  306. delLi(); 
  307.  
  308. //输入框获取焦点时样式 
  309. for (i = 0; i < aFtxt.length; i++) 
  310. { 
  311. EventUtil.addHandler(aFtxt[i], "focus", function () 
  312. {this.className = "active"});  
  313. EventUtil.addHandler(aFtxt[i], "blur", function () {this.className = ""}) 
  314. } 
  315.  
  316. //格式化时间, 如果为一位数时补0 
  317. function format(str) 
  318. { 
  319. return str.toString().replace(/^(/d)$/,"0$1") 
  320. } 
  321.  
  322. //头像 
  323. for (i = 0; i < aImg.length; i++) 
  324. { 
  325. aImg[i].onmouseover = function () 
  326. { 
  327. this.className += " hover" 
  328. }; 
  329. aImg[i].onmouseout = function () 
  330. { 
  331. this.className = this.className.replace(//s?hover/,"") 
  332. }; 
  333. aImg[i].onclick = function () 
  334. { 
  335. for (i = 0; i < aImg.length; i++) aImg[i].className = ""; 
  336. this.className = "current" 
  337.  
  338. } 
  339. } 
  340. }); 
  341. </script> 
  342. </head> 
  343.  
  344.  
  345. <body> 
  346. <div id="msgBox"> 
  347. <form> 
  348. <h2>来 , 说说你在做什么 , 想什么</h2> 
  349. <div> 
  350. <input id="userName" class="f-text" value="" /> 
  351. <p id="face"><img src="face1.gif" class="current" /><img src="face2.gif" /><img src="face1.gif" /><img src="face2.gif" /></p> 
  352. </div> 
  353. <div><input id="conBox" class="f-text"></div> 
  354. <div class="tr"> 
  355. <p> 
  356. <span class="countTxt">还能输入</span><strong class="maxNum">140</strong><span>个字</span> 
  357. <input id="sendBtn" type="button" value="" title="快捷键 Ctrl+Enter" /> 
  358. </p> 
  359. </div> 
  360. </form> 
  361. <div class="list"> 
  362. <h3><span>大家在说</span></h3> 
  363. <ul> 
  364. <li> 
  365. <div class="userPic"><img src="face.gif" /></div> 
  366. <div class="content"> 
  367. <div class="userName"><a href="javascript:;">日丶久生情</a>:</div> 
  368. <div class="msgInfo">新增Ctrl+Enter快捷键发送广播。</div> 
  369. <div class="times"><span>07月05日 12:20</span><a class="del" href="javascript:;">删除</a></div> 
  370. </div> 
  371. </li> 
  372. <li> 
  373. <div class="userPic"><img src="face.gif" /></div> 
  374. <div class="content"> 
  375. <div class="userName"><a href="javascript:;">日丶久生情</a>:</div> 
  376. <div class="msgInfo">新增选择头像功能。</div> 
  377. <div class="times"><span>07月05日 12:08</span><a class="del" href="javascript:;">删除</a></div> 
  378. </div> 
  379. </li> 
  380. <li> 
  381. <div class="userPic"><img src="face.gif" /></div> 
  382. <div class="content"> 
  383. <div class="userName"><a href="javascript:;">日丶久生情</a>:</div> 
  384. <div class="msgInfo">增加了记录广播时间的功能。</div> 
  385. <div class="times"><span>07月04日 16:55</span><a class="del" href="javascript:;">删除</a></div> 
  386. </div> 
  387. </li> 
  388. </ul> 
  389. </div>  
  390. </div> 
  391. </body> 
  392. </html> 


以上所述就是本文给大家分享的全部内容了,希望大家能够喜欢。

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

图片精选