Skip to content

Instantly share code, notes, and snippets.

@ihewro
Created March 3, 2018 11:21
Show Gist options
  • Save ihewro/5f6ec4f90305ee94458fcf26e768cad5 to your computer and use it in GitHub Desktop.
Save ihewro/5f6ec4f90305ee94458fcf26e768cad5 to your computer and use it in GitHub Desktop.
var txt_1 = '必须填写用户名',
txt_2 = '必须填写电子邮箱地址',
txt_3 = '邮箱地址不合法',
txt_4 = '必须填写评论内容';
function ajaxc() {
var $body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body');
var comments_order = 'DESC',
comment_list = '.comment-list',
comments = '#comments .comments-title',
comment_reply = '.comment-reply',
comment_form = '#comment_form',
respond = '#comments',
textarea = '#comment',
submit_btn = '#submit',
new_id = '',
parent_id = '';
var $msg = $('#msg');
click_bind();
/*var commentId_array = document.getElementsByTagName('html')[0].innerHTML.match(/id=\"?comment-\d+/g).join().match(/\d+/g).sort(function(a, b) {
return a - b
});
console.log("comment-id数组:" + commentId_array);*/
$(comment_form).submit(function() { // 提交
$msg.empty();
$(submit_btn).attr('disabled', true).fadeTo('slow', 0.5);
/* 预检 */
if ($(comment_form).find('#author')[0]) {
if ($(comment_form).find('#author').val() == '') {
toastr.info("请填写姓名");
msg_effect('#error');
return false;
}
if ($(comment_form).find('#mail').val() == '') {
toastr.info(txt_2);
msg_effect('#error');
return false;
}
var filter = /^[^@\s<&>]+@([a-z0-9]+\.)+[a-z]{2,4}$/i;
if (!filter.test($(comment_form).find('#mail').val())) {
toastr.info(txt_3);
msg_effect('#error');
return false;
}
}
var textValue = $(comment_form).find(textarea).val().replace(/(^\s*)|(\s*$)/g, "");//检查空格信息
if (textValue == null || textValue == "") {
toastr.info(txt_4);
msg_effect('#error');
return false;
}
$(submit_btn).addClass("active");
$('#spin').addClass("show inline");
//$('#loading').show();
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
data: $(this).serializeArray(),
error: function() {
toastr.info("提交失败,请重试!");
$msg.text('提交失败,请重试!');
msg_effect('#error');
return false;
},
success: function(data) { //成功取到数据
//console.log(data);
$(submit_btn).removeClass("active");
$('#spin').removeClass("show inline");
//$('#loading').slideUp();
try {
if (!$(comment_list, data).length) {
toastr.info("提交失败,可能输入内容不符合规则!");
$msg.text('提交失败,请重试!');
msg_effect('#error');
return false;
} else {
new_id = $(comment_list, data).html().match(/id=\"?comment-\d+/g).join().match(/\d+/g).sort(function(a, b) {
return a - b
}).pop(); // TODO:找新 id,如果在第二页评论的话,找到的ID是有问题的!
if ($('.page-navigator .prev').length && parent_id == ""){
new_id = '';
}//判断当前评论列表是否在第一页,并且只会在母评论时候才会生效
//console.log("new id " + new_id);
msg_effect("#success");
//插入评论内容到当前页面
if (parent_id) {
data = $('#comment-' + new_id, data).hide(); // 取新评论
if ($('#' + parent_id).find(".comment-children").length <= 0) {
$('#' + parent_id).append("<div class='comment-children list-unstyled m-l-xxl'><ol class='comment-list'></ol></div>");
}
if (new_id)//new_id不为空的时候才会插入
$('#' + parent_id + " .comment-children .comment-list").prepend(data);
//console.log('该评论为子评论,parent_id:' + parent_id);
parent_id = '';
//console.log(data);
} else {
data = $('#comment-' + new_id, data).hide(); // 取新评论
//console.log('该评论为母评论');
if (!$(comment_list).length) //如果一条评论也没有的话
$(respond).prepend('<h4 class="comments-title m-t-lg m-b special"> 0 条评论</h4><ol class="comment-list"><\/ol>'); // 加 ol
$(comment_list).prepend(data);
//console.log('评论内容:');
//console.log(data);
}
$('#comment-' + new_id).fadeIn(); // 显示
var num;
$(comments).length ? (num = parseInt($(comments).text().match(/\d+/)), $(comments).html($(comments).html().replace(num, num + 1))) : 0;
//console.log($('#comments h4').length);
// 评论数加一
TypechoComment.cancelReply();
$(textarea).val('');
$(comment_reply + ' a, #cancel-comment-reply-link').unbind('click');
click_bind(); // 新评论绑定
$(submit_btn).attr('disabled', false).fadeTo('slow', 1);
if (new_id){
$body.animate({scrollTop: $('#comment-' + new_id).offset().top - 50}, 500);
}else{
$body.animate({scrollTop: $('#comments').offset().top - 50}, 500);
}
}
} catch(e) {
//alert('评论ajax错误!请截图并联系主题制作者!\n\n' + e);
window.location.reload();
}
} // end success()
}); // end ajax()
return false;
}); // end $(comment_form).submit()
function click_bind() { // 绑定
$(comment_reply + ' a').click(function() { // 回复
//$body.animate({scrollTop: $(respond).offset().top - 180}, 400);
//console.log($(this).parent().parent().parent().parent());
parent_id = $(this).parent().parent().parent().parent().attr("id");
//console.log("parent_id:" + parent_id);
$(textarea).focus();
});
$('#cancel-comment-reply-link').click(function() { // 取消
parent_id = '';
});
}
function msg_effect(id) { // 出错
$(submit_btn).attr('disabled', false).fadeTo('', 1);
}
}
ajaxc();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment