DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript綜合知識 >> Js實現無刷新刪除內容
Js實現無刷新刪除內容
編輯:JavaScript綜合知識     

 本文給大家分享的是一段仿騰訊微博的無刷新刪除特效的代碼,非常的實用,有需要的小伙伴可以參考下。

   

Js實現無刷新刪除內容

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

以上所述就是本文給大家分享的全部內容了,希望大家能夠喜歡。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved