js实现简单的评论和回复功能(数组版)

var method={
  getDate:function (a,b){
        //获取当前日期
        //a表示年月日直接的分隔符,b表示时分秒之间的分隔符
        var dateStr="",
            nowT=new Date(),
            nowYear=nowT.getFullYear(),
            nowMonth=nowT.getMonth() + 1,
            nowDay=nowT.getDate(),
            nowHours=nowT.getHours(),
            nowMinites=nowT.getMinutes(),
            nowSeconds=nowT.getSeconds();
        if(a){
            dateStr= nowYear + a + method.zero(nowMonth) + a + method.zero(nowDay) + "    " + method.zero(nowHours) + b + method.zero(nowMinites) + b + method.zero(nowSeconds);
        }else{
            dateStr= nowYear + "年" + method.zero(nowMonth) + "月" + method.zero(nowDay) + "日" + "    " + method.zero(nowHours) + ":" + method.zero(nowMinites) + ":" + method.zero(nowSeconds);
        }
        return dateStr
    }  
}
method.js
DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>title>
    <link rel="stylesheet" href="css/public.css"/>
    <style>
        .outer{
            width:500px;
            margin:60px auto 0;
        }
        textarea{
            resize: none;
            width:100%;
            height:68px;
        }
        .btn,.replay-box-btn{
            width:80px;
            height:30px;
            margin-top: 20px;
            line-height:30px;
            text-align:center;
            border-radius: 5px;
            background: #00c1de;
            color:#fff;
            cursor: pointer;
        }
        .comment-con{
            margin-top: 20px;
        }
        h4{
            font-size:18px;
            font-weight: bold;
        }
        .comment-msg{
            width:380px;
        }
        .comment-name{
            font-weight: bold;
            margin-right:30px;
        }
        .comment-main p>span,.comment-main div>span{
            display: block;
            width: 80%;
            padding:5px;
            border-radius:5px;
            background:#ccc;
        }
        .comment-main p>span{
            float: left;
        }
        .comment-main div>span{
            float: right;
            color:#479EAB;
        }
        .comment-btn{
            width:46px;
            height:26px;
            text-align:center;
            line-height:26px;
            cursor: pointer;
            font-weight: bold;
        }
        .replay-box{
            display: none;
            width:100%;
        }
        span.comment-time{
            display: block;
            line-height: 26px;
            text-align: center;
        }
        .userImg,.manageImg{
            width: 60px;
            height:60px;
        }
    style>
head>
<body>
<form>
    <div class="outer">
        <div class="clearfix">
            <textarea placeholder="请输入评论内容">textarea>
            <div class="btn fr">提交div>
        div>
        <div class="comment-con">
            <h4>评论h4>
            <ul>ul>
        div>
    div>
form>
body>
<script src="js/jquery-1.11.3.min.js">script>
<script src="js/method.js">script>
<script>
    var commentJson=[
        {
            "id":0,
            "name":"用户a",
            "time":[
                "2016-01-01 12:00:00","2016-01-02 12:02:00","2016-01-03 12:03:00"
                ],
            "text":[
                {
                   "status":"0","val":"text1"//status为0表示用户留言,status为1表示管理员回复
                },
                {
                    "status":"1","val":"replay1"
                },
                {
                    "status":"1","val":"replay2"
                }
            ],
            "photo":"images/img1.png"
        }
    ];
    var $ul=$(".comment-con>ul")[0];
    show(commentJson);
   function show(ary){
       var str="";
       $(ary).each(function(i,item){
           str+="
  • "+item.id+">
    " + ""+item.name+"
    "; for(var x=0;x<item.text.length;x++){ str+=""+item.time[x]+""; if(item.text[x].status == "0"){ str+="

    "+item.text[x].val+"

    "; }else{ str+="
    "+item.text[x].val+"
    "; } } str+="
    回复
  • "; }); $(".comment-con>ul").html(str); } show(commentJson); $(".btn").on("click",function(){ var obj={},_val=$("textarea").val(),_name ="用户名",_time=method.getDate("-",":"),aryLen=commentJson.length,_photo="images/img1.png"; if(_val != ""){ obj={ "id":aryLen, "name":_name, "time":[_time], "text":[{"status":"0","val":_val}], "photo":_photo }; commentJson.push(obj); show(commentJson); $("textarea").val(""); }else{ alert("请输入评论信息") } }); $(".comment-con ul").delegate($("div.comment-btn"),"click",function(e){ var ev = e || window.event; var $this=$(ev.target); var replayStr="",manageImg="images/img1.png"; if($this.hasClass("comment-btn")){ if($(".replay-box").length == 0){ replayStr+="
    发布
    "; if($this.siblings(".replay-box").length > 0){ $this.siblings(".replay-box").css("display","block"); }else{ $(replayStr).appendTo($this.parents(".comment-msg")); $this.siblings(".replay-box").css("display","block"); } }else{ alert("您有未发表成功的评论,请发表成功后再回复新的评论") } } }).delegate($(".replay-box-btn"),"click",function(e){ var ev = e || window.event; var $this=$(ev.target); if($this.hasClass("replay-box-btn")){ var _value=$this.siblings('textarea').val(),replayTime=method.getDate("-",":"),_actIndex=$this.parents("li").attr("rel"); var replayObj={ "status":"1","val":_value }; if(_value != ""){ commentJson[_actIndex].time.push(replayTime); commentJson[_actIndex].text.push(replayObj); show(commentJson); $this.siblings('textarea').val(""); $this.parents(".replay-box").remove() }else{ alert("请填写回复内容") } } }) script> html>
    View Code

     

    转载于:https://www.cnblogs.com/dongxiaolei/p/7170638.html

    你可能感兴趣的:(js实现简单的评论和回复功能(数组版))