原生js實(shí)現(xiàn)回復(fù)評(píng)論功能
實(shí)現(xiàn)原理
功能1.刪除狀態(tài)
用removeChild()方法即可
功能2.最上面的點(diǎn)贊
判斷文字的內(nèi)容是否為贊,做相應(yīng)操作改變存放贊數(shù)量的容器文本內(nèi)容
功能3.回復(fù)評(píng)論
創(chuàng)建一個(gè)新的評(píng)論添加到評(píng)論列表里
功能4.回復(fù)里的點(diǎn)贊
判斷我是否點(diǎn)了贊,做相應(yīng)操作
功能5.回復(fù)或者刪除
判斷字符串回復(fù)還是刪除,做相應(yīng)操作
代碼用了事件代理,還有三元運(yùn)算判斷,減少代碼量
每行代碼都有詳細(xì)的注釋
一眼看到那么多的代碼不要煩躁
其實(shí)你要把每個(gè)功能單獨(dú)去看都是很簡(jiǎn)單的dom操作
一點(diǎn)點(diǎn)消化,讀懂每一行代碼
完整代碼
注:代碼復(fù)制到本地后替換下圖片
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
<style>
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;border:none; outline: none;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
address,cite,dfn,em,var{font-style:normal;}
code,kbd,pre,samp{font-family:courier new,courier,monospace;}
ul,ol{list-style:none;}
a{text-decoration:none;}
a:hover, a:visited, a:link, a:active{text-decoration:none;}
sup{vertical-align:text-top;}
sub{vertical-align:text-bottom;}
legend{color:#000;}
fieldset,img{border:0;}
button,input,select,textarea{font-size:100%;}
table{border-collapse:collapse;border-spacing:0;}
.clear{clear: both;float: none;height: 0;overflow: hidden;}
body{color:#333; font: 12px/1.3 'Arial','Microsoft YaHei';}
#pn{width: 550px; height: auto; background: #fff; margin: 0 auto; padding: 20px;}
.list0{padding: 20px 0; position: relative; border-top: 1px solid #eee;}
.head{width: 60px; float: left;}
.head img{width: 60px; height: 60px; }
.close{width: 20px; height: 20px; position: absolute; top: 0; right: 0; color: #696e78; font-size: 14px; text-align: center; line-height: 20px;}
.close:hover{color: #eb7350;}
.content{width: 450px; line-height: 20px; font-size: 14px; margin-left: 70px; }
.name{color: #eb7350; }
.pic{margin: 5px 0;}
.good:after{clear:both;content:'';display:block;width:0;height:0;visibility:hidden;}
.good{*zoom:1;}
.date{float:left; color: #808080;}
.good a{float: right; color: #808080;}
.people{background: #F7F7F7; height: 28px; line-height: 28px; padding-left: 10px; margin: 5px 0;}
.comment:after{clear:both;content:'';display:block;width:0;height:0;visibility:hidden;}
.comment{*zoom:1; padding: 10px 0; border-top: 1px solid #eee;}
.comment-left{width: 30px; float: left; display: inline; margin-right: 10px;}
.comment-left img{width: 30px; height: 30px;}
.comment-right{float: left; width: 410px;}
.comment-text{line-height: 18px;}
.comment-text span{color:#eb7350;}
.comment-date{font-size: 12px; line-height: 14px; color: #ccc; position: relative;}
.comment-zan{position: absolute; right: 40px; bottom: 0; color: #808080;}
.comment-dele{position: absolute; right: 0; bottom: 0; color: #808080;}
.hf:after{clear:both;content:'';display:block;width:0;height:0;visibility:hidden;}
.hf{*zoom:1;}
.hf-text{ border: 1px solid #eee; display: block; height: 15px; width: 438px; padding: 5px; resize: none; color: #ccc; font-size:12px;}
.hf-on .hf-text{height: 60px; color: #333; border:1px solid #ff8140;}
.hf-btn{float: right; width: 65px; height: 26px; background: #f7f7f7; color: #ccc; font-size: 12px; display: none;}
.hf-btn-on{background: #ff8140; color: #fff; }
.hf-nub{float: right; padding: 3px 5px; color: #666; display: none;}
.hf-on .hf-btn{display: inline;}
.hf-on .hf-nub{display: inline;}
</style>
</head>
<body>
<ul id="pn">
<li class="list0">
<a class="close" href="javascript:;">X</a>
<div class="head"><img src="images/T1.jpg" alt=""/></div>
<div class="content">
<p class="text"><span class="name">Andy:</span>哈哈哈哈哈誰(shuí)還沒個(gè)媽~//@我的朋友是個(gè)呆B: 飲水機(jī)那個(gè)蕾絲罩我給跪了//@八卦_我實(shí)在是太CJ了:仿佛看到了自己家</p>
<div class="pic"><img src="images/img1.jpg" alt=""/></div>
<div class="good"><span class="date">02-14 23:01</span><a class="dzan" href="javascript:;">贊</a></div>
<div class="people" total="2980">2980人覺得很贊</div>
<div class="comment-list">
<div class="comment" user="self">
<div class="comment-left"><img src="images/T1.jpg" alt=""/></div>
<div class="comment-right">
<div class="comment-text"><span class="user">老王:</span>我住隔壁我姓王</div>
<div class="comment-date">02-14 22:00
<a class="comment-zan" href="javascript:;" total="23" my="1">23 取消贊</a>
<a class="comment-dele" href="javascript:;">回復(fù)</a>
</div>
</div>
</div>
<div class="comment" user="self">
<div class="comment-left"><img src="images/T1.jpg" alt=""/></div>
<div class="comment-right">
<div class="comment-text"><span class="user">我:</span>看哭了留卡號(hào)吧</div>
<div class="comment-date">02-14 24:00
<a class="comment-zan" href="javascript:;" total="0" my="0">贊</a>
<a class="comment-dele" href="javascript:;">刪除</a>
</div>
</div>
</div>
</div>
<div class="hf">
<textarea type="text" class="hf-text" autocomplete="off" maxlength="100">評(píng)論…</textarea>
<button class="hf-btn">回復(fù)</button>
<span class="hf-nub">0/100</span>
</div>
</div>
</li>
<li class="list0">
<a class="close" href="javascript:;">X</a>
<div class="head"><img src="images/T2.jpg" alt=""/></div>
<div class="content">
<p class="text"><span class="name">Andy:</span>哈哈哈哈哈誰(shuí)還沒個(gè)媽~//@我的朋友是個(gè)呆B: 飲水機(jī)那個(gè)蕾絲罩我給跪了//@八卦_我實(shí)在是太CJ了:仿佛看到了自己家</p>
<div class="pic"><img src="images/img1.jpg" alt=""/></div>
<div class="good"><span class="date">02-14 23:01</span><a class="dzan" href="javascript:;">贊</a></div>
<div class="people" total="0" style="display: none;"></div>
<div class="comment-list">
<div class="comment" user="self">
<div class="comment-left"><img src="images/T2.jpg" alt=""/></div>
<div class="comment-right">
<div class="comment-text"><span class="user">我:</span>看哭了留卡號(hào)吧</div>
<div class="comment-date">02-14 24:00
<a class="comment-zan" href="javascript:;" total="286" my="1">286 取消贊</a>
<a class="comment-dele" href="javascript:;">刪除</a>
</div>
</div>
</div>
</div>
<div class="hf">
<textarea type="text" class="hf-text" autocomplete="off" maxlength="100">評(píng)論…</textarea>
<button class="hf-btn">回復(fù)</button>
<span class="hf-nub">0/100</span>
</div>
</div>
</li>
</ul>
<script type="text/javascript">
//在頁(yè)面加載完后立即執(zhí)行多個(gè)函數(shù)。
function addloadEvent(func){
var oldonload=window.onload;
if(typeof window.onload !="function"){
window.onload=func;
}
else{
window.onload=function(){
if(oldonload){
oldonload();
}
func();
}
}
}
addloadEvent(b);
function b(){
var pn=document.getElementById("pn");
var lists=pn.children;
//刪除當(dāng)前節(jié)點(diǎn)
function remove(node){
node.parentNode.removeChild(node);
}
//上面的點(diǎn)贊
function praisebox(box,el){
//獲取贊數(shù)量容器
var praise=box.getElementsByClassName("people")[0];
//獲取容器當(dāng)前total值
var total=parseInt(praise.getAttribute("total"));
//獲取點(diǎn)擊的innerHTML
var txt=el.innerHTML;
//創(chuàng)建一個(gè)新的total存儲(chǔ)用
var newtotal;
//判斷點(diǎn)擊的文字內(nèi)容
if(txt=="贊"){
//total值+1 因?yàn)槲疫€沒點(diǎn)擊贊,所以要點(diǎn)擊的時(shí)候就多了一個(gè)人 total+1
newtotal=total+1;
//判斷贊數(shù)量 把相應(yīng)文字放到贊容器里
praise.innerHTML=newtotal==1 ? "我覺得很贊" : "我和" + total +"個(gè)人覺得很贊";
el.innerHTML="取消贊";
}
else{
//反之total值-1
newtotal=total-1;
praise.innerHTML=newtotal==0 ? "" : newtotal +"個(gè)人覺得很贊";
el.innerHTML="贊";
}
//更新total值
praise.setAttribute("total",newtotal);
//如果沒有人點(diǎn)贊容器隱藏
praise.style.display=(newtotal==0) ? "none" : "block";
}
//回復(fù)評(píng)論
function reply(box){
//獲取評(píng)論框
var textarea=box.getElementsByTagName("textarea")[0];
//獲取包含所有評(píng)論的容器
var comment=box.getElementsByClassName("comment-list")[0];
//創(chuàng)建新的評(píng)論div
var div=document.createElement("div");
//賦類名
div.className="comment";
//設(shè)置屬性
div.setAttribute("user","self");
//獲取每條評(píng)論的innerHTML結(jié)構(gòu),每次只替換textarea的輸入內(nèi)容和 當(dāng)前發(fā)送時(shí)間
var html='<div class="comment-left">'+'<img src="images/T2.jpg" alt=""/>'+'</div>'+
'<div class="comment-right">'+
'<div class="comment-text"><span>我:</span>'+textarea.value+'</div>'+
'<div class="comment-date">'+
getTime()+
'<a class="comment-zan" href="javascript:;" total="0" my="0">贊</a>'+
'<a class="comment-dele" href="javascript:;">刪除</a>'+
'</div>'+
'</div>';
//插入到新建的評(píng)論div
div.innerHTML=html;
//把新評(píng)論插入到評(píng)論列表
comment.appendChild(div);
//評(píng)論后初始化textarea輸入框
textarea.value="評(píng)論…";
textarea.parentNode.className="hf";
}
//獲取當(dāng)前時(shí)間回復(fù)評(píng)論時(shí)調(diào)用
function getTime(){
var t=new Date();
var y=t.getFullYear();
var m=t.getMonth()+1;
var d=t.getDate();
var h=t.getHours();
var mi=t.getMinutes();
m=m<10?"0"+m:m;
d=d<10?"0"+d:d;
h=h<10?"0"+h:h;
mi=mi<10?"0"+mi:mi;
return y+"-"+m+"-"+d+""+h+":"+mi;
}
//回復(fù)里點(diǎn)贊
function praiseReply(el){
//獲取當(dāng)前total值 也就是所有點(diǎn)贊數(shù)量
var total=parseInt(el.getAttribute("total"));
//獲取當(dāng)前my值 我的點(diǎn)贊
var my=parseInt(el.getAttribute("my"));
//創(chuàng)建新的total
var newtotal;
//判斷my=0就是我準(zhǔn)備要點(diǎn)贊
if(my==0){
//我點(diǎn)了贊總數(shù)量就要+1
newtotal=total+1;
//更新total值
el.setAttribute("total",newtotal);
//更新my值
el.setAttribute("my",1);
//更新文字 就是我點(diǎn)了后 文字就是取消贊了
el.innerHTML=newtotal+" 取消贊";
}else{
//反之-1
newtotal=total-1;
el.setAttribute("total",newtotal);
el.setAttribute("my",0);
el.innerHTML=(newtotal==0)?" 贊":newtotal+" 贊";
}
}
//操作回復(fù)
function operateReply(el){
//每條評(píng)論
var comment=el.parentNode.parentNode.parentNode;
//整個(gè)狀態(tài)
var box=comment.parentNode.parentNode.parentNode;
//評(píng)論框
var textarea=box.getElementsByTagName("textarea")[0];
//名字
var user=comment.getElementsByClassName("user")[0];
//點(diǎn)擊的innerHTML
var txt=el.innerHTML;
//判斷當(dāng)前點(diǎn)擊的是否為回復(fù)
if(txt=="回復(fù)"){
//評(píng)論框觸發(fā)焦點(diǎn)事件 也就是變高
textarea.onfocus();
//內(nèi)容變?yōu)榛貜?fù)+當(dāng)前人的名字
textarea.value="回復(fù) "+user.innerHTML;
//調(diào)用鍵盤事件
textarea.onkeyup();
}else{
//否則就是刪除節(jié)點(diǎn)
remove(comment);
}
}
//遍歷所有狀態(tài)消息
for(var i=0;i<lists.length;i++){
//全部事件代理
lists[i].onclick=function(e){
//獲取當(dāng)前點(diǎn)擊事件
var e=e||window.event;
var el=e.srcElement;
if(!el){
el=e.target;//兼容火狐
}
//判斷點(diǎn)擊的類名
switch(el.className){
//關(guān)閉整個(gè)狀態(tài)
case "close":
remove(el.parentNode);
break;
//上面的點(diǎn)贊
case "dzan":
praisebox(el.parentNode.parentNode.parentNode,el);
break;
//回復(fù)評(píng)論
case "hf-btn hf-btn-on":
reply(el.parentNode.parentNode.parentNode);
break;
//每條評(píng)論中點(diǎn)贊
case "comment-zan":
praiseReply(el);
break;
case "comment-dele":
operateReply(el);
break;
}
}
var textarea=lists[i].getElementsByClassName("hf-text")[0];
//焦點(diǎn)事件
textarea.onfocus=function(){
this.parentNode.className='hf hf-on';
this.value = this.value == '評(píng)論…' ? '' : this.value;
}
//失焦事件
textarea.onblur=function(){
if(this.value==''){
this.parentNode.className='hf';
this.value ='評(píng)論…';
}
}
//鍵盤事件
textarea.onkeyup=function(){
var len=this.value.length;
var textParentNode=this.parentNode;
var textBtn=textParentNode.children[1];
var textNub=textParentNode.children[2];
if(len==0 /*|| len>100*/){
textBtn.className="hf-btn";
}else{
textBtn.className="hf-btn hf-btn-on";
/*this.style.color="#333"; */
}
textNub.innerHTML=len+"/100";
}
}
//遍歷結(jié)束
}
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- JavaScript評(píng)論點(diǎn)贊功能的實(shí)現(xiàn)方法
- vue.js評(píng)論發(fā)布信息可插入QQ表情功能
- Vue.js實(shí)現(xiàn)文章評(píng)論和回復(fù)評(píng)論功能
- JS類定義原型方法的兩種實(shí)現(xiàn)的區(qū)別評(píng)論很多
- vue.js實(shí)現(xiàn)用戶評(píng)論、登錄、注冊(cè)、及修改信息功能
- 使用AngularJS和PHP的Laravel實(shí)現(xiàn)單頁(yè)評(píng)論的方法
- JavaScript實(shí)現(xiàn)簡(jiǎn)單評(píng)論功能
- js寫的評(píng)論分頁(yè)(還不錯(cuò))
- react結(jié)合bootstrap實(shí)現(xiàn)評(píng)論功能
相關(guān)文章
JavaScript知識(shí)點(diǎn)總結(jié)之如何提高性能
JavaScript的性能問題不容小覷,這就需要我們開發(fā)人員在編寫JavaScript程序時(shí)多注意一些細(xì)節(jié),本文非常詳細(xì)的介紹了一下JavaScript性能優(yōu)化方面的知識(shí)點(diǎn),絕對(duì)是干貨,需要的朋友快來(lái)一起學(xué)習(xí)吧2016-01-01
JavaScript如何從listbox里同時(shí)刪除多個(gè)項(xiàng)目
要從列表框同時(shí)刪除多個(gè)項(xiàng)目只能從下向上刪除,這樣就不會(huì)出現(xiàn)索引號(hào)亂變的問題了,下面有個(gè)不錯(cuò)的示例,大家可以感受下2013-10-10
Javascript實(shí)現(xiàn)字?jǐn)?shù)統(tǒng)計(jì)
現(xiàn)在流行的Twitter等微博客網(wǎng)站,有一個(gè)很好的用戶體驗(yàn),就是在文本框中輸入文字的時(shí)候,會(huì)自動(dòng)統(tǒng)計(jì)輸入的字符,并顯示用戶還能輸入的字符,在限制了140個(gè)字的微博客中,這樣的小提示可以很好的增強(qiáng)用戶體驗(yàn)。2015-07-07
JS控制一個(gè)DIV層在指定時(shí)間內(nèi)消失的方法
這篇文章主要介紹了JS控制一個(gè)DIV層在指定時(shí)間內(nèi)消失的方法,需要的朋友可以參考下2014-02-02
JS中數(shù)據(jù)類型的正確判斷方法實(shí)例
怎么去判斷一個(gè)數(shù)據(jù)屬于哪個(gè)數(shù)據(jù)類型,這個(gè)是很常見的操作,我們一般都會(huì)想到typeof和instanceof這兩個(gè)常見的方法,但有時(shí)候這兩種方法并不能滿足我們的需求,下面這篇文章主要給大家介紹了關(guān)于JS中數(shù)據(jù)類型的正確判斷方法,需要的朋友可以參考下2021-08-08
JavaScript實(shí)現(xiàn)將阿拉伯?dāng)?shù)字轉(zhuǎn)換成中文或大寫中文的方法
現(xiàn)在有需求將億元之內(nèi)的阿拉伯?dāng)?shù)字轉(zhuǎn)換成中文,例如:1234轉(zhuǎn)換后變?yōu)橐磺Ф偃脑俎D(zhuǎn)換成壹仟貳佰叁拾肆,所以本文給大家介紹了用JavaScript實(shí)現(xiàn)將阿拉伯?dāng)?shù)字轉(zhuǎn)換成中文大寫,感興趣的小伙伴跟著小編一起來(lái)看看吧2024-11-11
用jscript實(shí)現(xiàn)列出安裝的軟件列表
用jscript實(shí)現(xiàn)列出安裝的軟件列表...2007-06-06
js 無(wú)提示關(guān)閉瀏覽器頁(yè)面的代碼
js 無(wú)提示關(guān)閉瀏覽器頁(yè)面的代碼與函數(shù)需要的朋友可以參考下。2010-03-03
JS使用getComputedStyle()方法獲取CSS屬性值
經(jīng)常會(huì)用到j(luò)s來(lái)獲取元素的CSS樣式,由于方法眾多,在下面的文章中為大家詳細(xì)整理下2014-04-04

