jQuery實現簡單評論區功能
阿新 • • 發佈:2020-10-27
本文例項為大家分享了jQuery實現簡單評論區的具體程式碼,供大家參考,具體內容如下
直接看程式碼吧
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link rel="stylesheet" href="css/weibo.css" > </head> <body> <div class="w"> <!-- 操作的介面 --> <div class="controls"> <img src="images/tip.png" alt="jQuery實現簡單評論區功能"><br> <textarea placeholder="說點什麼吧..." id="area" cols="30" rows="10"></textarea> <div> <span class="useCount">0</span> <span>/</span> <span>200</span> <button id="send">釋出</button> </div> </div> <!-- 微博內容列表 --> <div class="contentList"> <ul> </ul> </div> </div> <script src="js/jquery.min.js"></script> <script src="js/weibo.js"></script> </body> </html>
* { margin: 0; padding: 0; } ul { list-style: none; } .w { width: 900px; margin:0 auto; } .controls textarea { width: 878px; height: 100px; resize: none; border-radius: 10px; outline:none; padding-left: 20px; padding-top:10px; font-size: 18px; } .controls { overflow: hidden; } .controls div { float: right; } .controls div span { color:#666; } .controls div .useCount { color:red; } .controls div button { width: 100px; outline: none; border:none; background: rgb(0,132,255); height: 30px; cursor: pointer; color:#fff; font:bold 14px '宋體'; transition: all 0.5s; } .controls div button:hover { background: rgb(0,225,255); } .controls div button:disabled { background: rgba(0,255,0.5); } .contentList { margin-top:50px; position: relative; } .contentList li { padding: 20px 0; position: relative; opacity: 0; border-bottom: 1px dashed #ccc; } .contentList li .info { position: relative; } .contentList li .info span { position: absolute; top:15px; left:100px; font:bold 16px '宋體'; } .contentList li .info p { position: absolute; top:40px; left: 100px; color:#aaa; font-size: 12px; } .contentList img { width: 80px; border-radius: 50%; } .contentList li .content { padding-left: 100px; color: #666; word-break: break-all; } .contentList li button { width: 50px; height: 30px; text-align: center; line-height: 30px; color: white; background-color: #0084FF; border: 0; outline: none; cursor: pointer; position: absolute; right: 0; bottom: 10px; } .contentList li button:disabled{ background: rgba(0,0.5); } .contentList li button:hover { background: rgb(0,255); }
// ①點擊發布按鈕, 動態建立一個小li,放入文字框的內容和刪除按鈕, 並且新增到ul 中。 // // ②點選的刪除按鈕,可以刪除當前的微博留言。 //jQuery入口 $(function () { //名字陣列 var nameArr = ["百里守約","孫悟空","紫霞","安琪拉","妲己"]; //名字對應下標 也是要生成的隨機數的陣列 var newArr = []; //本地儲存資料 物件陣列 var bd_arr = []; //每次重新整理頁面 或者一進入頁面 有歷史記錄就要顯示出來 getItem(); //釋出按鈕 用on()繫結點選事件 $("#send").on("click",function () { //檢測有沒有輸入內容 有內容允許釋出 否則提示 if ($(this).parents().siblings("#area").val() == "") { alert("少俠,寫點什麼再發布吧~"); } else { //獲取要儲存的新的資料 var name = nameArr[arfa()]; var time = getTime(); var nr = $(this).parents().siblings("#area").val(); //要儲存的資料 以物件的形式放在數組裡 bd_arr.push({name: name,time: time,nr: nr}); //轉成字串 var str = JSON.stringify(bd_arr); //向本地申請空間 存起來 localStorage.setItem('li',str); //重新整理資料 再顯示最新的所有li getItem(); //文字框置空 $("#area").val(""); //輸入的字元置0 $(".useCount").html("0"); //釋出完成 禁用按鈕 $("#send").prop("disabled",true); } }); //可以繫結多個事件 用物件的方式 輸入框繫結input,focus,blur事件 $("#area").on({ input: function () { // 輸入內容小於0禁用釋出按鈕 if ($(this).val().length === 0) { $(".useCount").html("0"); $("#send").prop("disabled",true); } else if ($(this).val().length > 200) { //大於最大輸入值 只取前200個字元做有效值 $(this).val($(this).val()?.substring(0,200)); } else { //在有效範圍內 解禁釋出按鈕 $("#send").prop("disabled",false); //實時顯示使用者輸入的字元數 $(".useCount").html($(this).val().length); } },focus: function () { //重新獲得焦點 解禁釋出按鈕 禁用刪除按鈕 $("#send").prop("disabled",false); $("li").each(function (index,ele) { $(ele).find("#remove").prop("disabled",true); }); },blur: function () { //失去焦點 解禁 刪除按鈕 $("li").each(function (index,false); }); } }); //獲取當時時間 function getTime() { var data = new Date(); return (data.getFullYear() + "-" + (data.getMonth() + 1) + "-" + data.getDate() + " " + data.getHours() + "時" + data.getMinutes() + "分" + data.getSeconds() + "秒"); } //生成隨機數 去重 function arfa() { if (newArr.length === 0) { for (var i = 0; i < nameArr.length; i++) { newArr[newArr.length] = i; } } var num = Math.floor(Math.random() * nameArr.length); while (1) { if (newArr.indexOf(num) !== -1) { newArr.splice(newArr.indexOf(num),1); break; } else { num = Math.floor(Math.random() * nameArr.length); continue; } } return num; } //讀取本地資料 function getItem() { var name_arr = []; var time_arr = []; var nr_arr = []; var li_str = null; //讀取本地資料 var str = localStorage.getItem('li'); if (str != null) { //字串陣列轉換為 物件陣列 bd_arr = JSON.parse(str); // 迴圈遍歷 for (var i = 0; i < bd_arr.length; i++) { // 取出一個物件 var obj = bd_arr[i]; // {name:"...",time:"...",nr:"。。。"} name_arr[name_arr.length] = obj.name; time_arr[time_arr.length] = obj.time; nr_arr[nr_arr.length] = obj.nr; } //根據取出的資料 動態建立li for (var i = 0; i < name_arr.length; i++) { li_str = "<li>" + "<div class='info'>" + "<img src='images/03.jpg'>" + "<span>" + name_arr[i] + "</span>" + "<p>" + "釋出於:" + time_arr[i] + "</p>" + "</div>" + "<div class='content'>" + nr_arr[i] + "</div>" + "<button id='remove'>" + "刪除" + "</button>" + "</li>" + li_str ; } //因為有資料更新要覆蓋顯示 所以用了html方式新增li fadeTo()淡入效果 $("ul").html($(li_str)).children().stop().fadeTo(1000,1); //在剛進入頁面 沒有釋出按鈕的點選事件時 刪除按鈕也要好用 所以這裡也要繫結點選事件 //給刪除按鈕繫結 點選事件 因為li動態生成的 要在生成之後立馬繫結事件 $("li button").each(function (i,e) { $(e).on("click",function () { $(this).parents("li").remove(); //要刪除的資料 在數組裡找到並刪除 bd_arr.pop({ name: $(this).parents("li").find(".info span").html(),time: $(this).parents("li").find(".info p").html().substr(4),nr: $(this).parents("li").find(".content").html() }); //轉成字串 str = JSON.stringify(bd_arr); //覆蓋刪除前的資料 localStorage.setItem('li',str); }); }); } } });
其實這個小案例的核心呢就是jQuery動態建立,localStorage本地儲存,本地資料的存入和取出,要用JSON.parse()和JSON.stringify()來進行轉換,然後我是用了物件陣列的方式儲存的,然後有新資料要存入和有資料要被刪除時用了push()和pop(),要注意陣列中的每一個都是一個物件等等…
然後就是各種用on()繫結事件,還有動態建立的元素,要注意繫結事件的時機,事件處理無非就寫了控制最大輸入字元數,必須輸入一些才能點擊發布,文字框獲得焦點和失去焦點啟用或者禁用哪個按鈕什麼的等等。
看看效果吧,我多錄了兩個效果圖,感覺整體還算可以,但是程式碼還是有超級大優化空間的,就暫時不要在意這麼多好了~~~慢慢來
好了,就這些
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。