function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; }; $(function(){ function validateTextarea(val){ if(val.length < 3){ Dml.fun.showTipsDialog({ title: '错误提示', h2: '评论字数不能少于3个字符!', type: 'failbox' }); return false; } return true; } var userAvatar = $('#userAvatar').val(); //textarea绑定focus,blur和字数 $('.comenlist').on('focus','.wordbox',function(){ $(this).parent('.text-box').addClass('text-box-on'); $(this).keyup(); }); $('.comenlist').on('input propertychange','.wordbox',function(){ var val = this.value; var len = val.length; var els = this.parentNode.children; var btn = els[1]; var word = els[2]; if (len <= 0 ) { word.innerHTML = '您没有输入内容'; $(btn).addClass('btn-off'); }else if(len > 140){ word.innerHTML = '您已超出字数'; btn.style.background='#ccc'; }else{ word.innerHTML = len + '/140'; btn.style.background='#717171'; } }); $('.praisebtn').click(function(){ var txt = $(this).html(); var btn = $(this); var praisesTotal = $(this).parent('div').find('.praiseword'); var oldTotal = parseInt(praisesTotal.attr('total')); var newTotal; var newTotal2; var request_url = "/common/addpraise/" var typeid = 2 if($(this).hasClass("collected")){ request_url = "/common/delpraise/" } var csrftoken = getCookie('csrftoken'); $.ajax({ type:"POST", url: request_url, dataType:"json", async: true, data:{ typeid: typeid, favid: $(this).attr("data"), }, success:function(data){ if (!btn.hasClass('collected')) { newTotal = oldTotal + 1; praisesTotal.attr('total', newTotal); if(newTotal == 1){ praisesTotal.html('我觉得很赞'); }else{ praisesTotal.html(newTotal + '个人觉得很赞'); } btn.html('取消赞').addClass('collected'); }else{ newTotal2 = oldTotal-1; praisesTotal.attr('total', newTotal2); if(newTotal == 0){ praisesTotal.html('还没有人点赞'); }else{ praisesTotal.html( newTotal2 + '个人觉得很赞'); } btn.html('点赞').removeClass('collected'); } }, beforeSend: function(xhr, settings) { xhr.setRequestHeader("X-CSRFToken", csrftoken); } }); }); //是否登录控制 if(isLogin == 'True'){ $('.comenlist').on('mouseover','.btn',function(){ $(this).parents('.textinput').find('.wordbox').blur().css('height','60px'); }); //评论楼主 $('.commentbtn').on('click',function(){ var _self = $(this), textarea = _self.parent().find('textarea:first'), comment = textarea.val(), diary_id = _self.attr("diaryid"), csrftoken = getCookie('csrftoken'), validate = validateTextarea(comment); if(!validate){ return false; } $.ajax({ type: "POST", url:"/diary/add_comment/", data:{ comment:comment, diaryid:diary_id, }, success: function(data) { if (data.id){ var textareaval = textarea.val(), len = textareaval.length, evalbox = '
我 回复 '+ parentName +':' + textareaval + '
' + ''+ Dml.fun.getDate() +'
' + '回复
' + '我' +' 回复 '+ parentName +': '+ textareaval2 + '
' + ''+ Dml.fun.getDate() +'
' + ''+ '
我评论:' + textareaval + '
' + ''+ Dml.fun.getDate() +'
' + '回复
'+ '