typecho模板ajax,Typecho 实现 ajax 评论功能

Typecho 评论实现 Ajax 有个插件 AjaxComments ,不过有些网友反馈说该插件有小 bug,不太好用,所以就产生了下面的这段代码,大家可以直接复制以下的修改版代码,在公共 js 中调用 ajaxComments() 方法即可。function ajaxComment() {

var selector = {

commentMainFrame: '#comment',

commentList: '#commentlist',

commentNumText: '#comment h3',

commentReplyButton: '#comment span.reply',

submitForm: '#commentform',

submitTextarea: '#textarea',

submitButton: '#submit',

};

var parentId = '';

bindCommentReplyButton();

$(selector.submitTextarea).after('

');

$msg = $('#ajaxCommentMsg');

$(document).on('submit', selector.submitForm, function() {

$msg.empty();

$(selector.submitButton).val('发射中哦=A=');

$(selector.submitButton).attr('disabled', true).fadeTo('slow', 0.5);

if ($(selector.submitForm).find('#author')[0]) {

if ($(selector.submitForm).find('#author').val() == '') {

message('昵称没填呢QAQ');

enableCommentButton();

return false;

}

if ($(selector.submitForm).find('#mail').val() == '') {

message('邮箱没填呢QAQ');

enableCommentButton();

return false;

}

var filter = /^[^@\s]+@([a-z0-9]+\.)+[a-z]{2,4}$/i;

if (!filter.test($(selector.submitForm).find('#mail').val())) {

message('邮箱地址不正确呢QAQ');

enableCommentButton();

return false;

}

}

if ($(selector.submitForm).find(selector.submitTextarea).val() == '') {

message('评论似乎什么也没写呢QAQ');

enableCommentButton();

return false;

}

$.ajax({

url: $(this).attr('action'),

type: $(this).attr('method'),

data: $(this).serializeArray(),

error: function() {

message('发射失败,请重试!');

setTimeout(NProgress.done, 500)

enableCommentButton();

return false;

},

success: function(data) {

if (!$(selector.commentList, data).length) {

errorMsg = data.match(/.+/g).join().match(/\

.+\/g).join().match(/[^\,]+/g);

$msg.html(errorMsg[0] + errorMsg[1] + errorMsg[2]);

enableCommentButton();

return false;

} else {

userCommentId = $(selector.commentList, data).html().match(/id=\"?comment-\d+/g).join().match(/\d+/g).sort(function(a, b) {

return a - b;

}).pop();

commentLi = '

' + $('#comment-' + userCommentId, data).html(); + '';

if (parentId) {

if ($('#' + parentId).find(".comment-children").length <= 0) {

$('#' + parentId).append("

}

$('#' + parentId + " .children:first").append(commentLi);

parentId = ''

$body.animate({

scrollTop: $('#comment-' + userCommentId).offset().top - 450

}, 900);

} else {

$(selector.commentList).prepend(commentLi)

$body.animate({

scrollTop: $('#comment-' + userCommentId).offset().top - 200

}, 900);

}

//$('#comment-' + userCommentId).slideDown('slow');

//console.log(userCommentId);

$(selector.commentNumText).length ? (n = parseInt($(selector.commentNumText).text().match(/\d+/)), $(selector.commentNumText).html($(selector.commentNumText).html().replace(n, n + 1))) : 0;

TypechoComment.cancelReply();

$(selector.submitTextarea).val('');

$(selector.commentReplyButton + ' b, #cancel-comment-reply-link').unbind('click');

bindCommentReplyButton();

enableCommentButton();

}

}

});

return false;

});

function bindCommentReplyButton() {

$(document).on('click', selector.commentReplyButton, function() {

parentId = $(this).parents('li.comment').attr("id");

$(selector.submitTextarea).focus();

});

$(document).on('click', '#cancel-comment-reply-link', function() {

parentId = '';

});

}

function enableCommentButton() {

$(selector.submitButton).attr('disabled', false).fadeTo('', 1);

$(selector.submitButton).val('发射=A=');

}

function message(msg) {

$msg.hide();

$msg.html(msg).slideToggle('fast');

}

}

本文检索关键词:开发日志

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值