preface
-
The following contents may need to add callback functions for sites that have achieved the effect of Ajax. -
The following content is completely applicable to the implementation of the instantclick site -
Contents marked with (*) are optional and can be omitted -
If you have any questions, you can send an email or comment directly, Don't ask me questions on QQ
code analysis
Basic knowledge of ajax
$.ajax({ Url:,//The address to send the request Type:,//Request mode, post or get request Data:,//Data to be sent to the server error: function() { //Callback function for failed request execution }, success: function(data) { //The callback function for successful request execution. The parameter data is the plain text HTML code of the page after successful request } })
Implementation steps
monitor
comment_form Submission event of comment box :
$(comment_form).submit(function() { //Submit Event })
(*) Check whether the comment content and the author's email content comply with the rules (pre check) :
Initiate an ajax request :
$.ajax({ Url: $(this). attr ('action '),//submit the action attribute of the form Type: $(this). attr ('method '),//submit the method attribute of the table form Data: $(this). serializeArray(),//Data to be sent to the server error: function() { //Write the prompt code here }, success: function(data) { //This part will be explained in Step 4 } })
Get the data of the ajax request, find the content of the latest comment from the data, and insert the content into the current page :
-
from In the data returned by the successful ajax request Find Latest comments Insert into the current page. -
Directly at the time of submission, Take a variable to save the content submitted by the user, and then directly assemble a comment according to your comment structure Insert into the current page
-
First question: understand what data is returned after a successful ajax request?
-
The second question: How to find the content of the latest comment and where to insert it?
-
Parent: directly comment on the -
Children: a comment in a comment article
new_id = $(comment_list, data).html().match(/id=\"?comment-\d+/g).join().match(/\d+/g).sort(function(a, b) { return a - b }).pop();
if ($('.page-navigator .prev').length && parent_id == ""){ new_id = ''; }//Make a judgment ... If (new_id)//It will be inserted only when the new_id is not empty $('#' + parent_id + " .comment-children .comment-list").prepend(data);
$(comment_list).prepend(data);
$(comment_reply+'a'). click (function() {//Reply parent_id = $(this).parent().parent().parent().parent().attr("id"); $(textarea).focus(); }); $('# cancel comment reply link'). click (function() {//Cancel parent_id = ''; });
if (parent_id) { data = $('#comment-' + new_id, data).hide(); // Get new comments 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)//It will be inserted only when the new_id is not empty $('#' + parent_id + " .comment-children .comment-list").prepend(data); //Console. log ('This comment is a sub comment, parent_id:' + parent_id); parent_id = ''; //console.log(data); }
Code download