(function($){ function crateCommentInfo(obj){ if(typeof(obj.time) == "undefined" || obj.time == ""){ obj.time = getNowDateFormat(); } var el = "

"+obj.replyName+"

" +"
"+obj.time+""; if(typeof(obj.address) != "undefined" && obj.browse != ""){ el =el+""+obj.address+""; } el = el+"

"+obj.content+"

"; if(obj.replyBody != "" && obj.replyBody.length > 0){ var arr = obj.replyBody; for(var j=0;j
"; return el; } //返回每个回复体内容 function createReplyComment(reply){ var replyEl = "
"+reply.replyName+":@"+reply.beReplyName+""+reply.content+"
" + "

"+reply.time+" 回复

"; return replyEl; } function getNowDateFormat(){ var nowDate = new Date(); var year = nowDate.getFullYear(); var month = filterNum(nowDate.getMonth()+1); var day = filterNum(nowDate.getDate()); var hours = filterNum(nowDate.getHours()); var min = filterNum(nowDate.getMinutes()); var seconds = filterNum(nowDate.getSeconds()); return year+"-"+month+"-"+day+" "+hours+":"+min+":"+seconds; } function filterNum(num){ if(num < 10){ return "0"+num; }else{ return num; } } function replyClick(el){ el.parent().parent().append("
发送
") .find(".send").click(function(){ var content = $(this).prev().val(); if(content != ""){ var parentEl = $(this).parent().parent().parent().parent(); var obj = new Object(); obj.replyName="匿名"; if(el.parent().parent().hasClass("reply")){ console.log("1111"); obj.beReplyName = el.parent().parent().find("a:first").text(); }else{ console.log("2222"); obj.beReplyName=parentEl.find("h3").text(); } obj.content=content; obj.time = getNowDateFormat(); var replyString = createReplyComment(obj); $(".replybox").remove(); parentEl.find(".reply-list").append(replyString).find(".reply-list-btn:last").click(function(){alert("不能回复自己");}); }else{ alert("空内容"); } }); } $.fn.addCommentList=function(options){ var defaults = { data:[], add:"" } var option = $.extend(defaults, options); //加载数据 if(option.data.length > 0){ var dataList = option.data; var totalString = ""; for(var i=0;i 0){ $(".replybox").remove(); }else{ $(".replybox").remove(); replyClick($(this)); } }); $(document).on("click",".reply-list-btn",function(){ if($(this).parent().parent().find(".replybox").length > 0){ $(".replybox").remove(); }else{ $(".replybox").remove(); replyClick($(this)); } }) } //添加新数据 if(option.add != ""){ obj = option.add; var str = crateCommentInfo(obj); $(this).prepend(str).find(".reply-btn").click(function(){ replyClick($(this)); }); } } })(jQuery);