简体   繁体   English

Ajax请求后的CSS选择器(注释系统)

[英]css selectors after ajax request (comments system)

Problem: 问题:

After adding a comment on a webpage everything works property, I mean added comments but I want them to slide down the last added comment. 在网页上添加评论后,everything works属性就可以了,我的意思是添加评论,但我希望它们向下滑动最后添加的评论。 I think it is problem with css selectors, comments should be over last comments on website. 我认为这是CSS选择器的问题,评论应该超过网站上的最后评论。 Which one selector do I have to type for it? 我必须输入哪个选择器?

Anybody can help me with it? 有人可以帮我吗? I hope all is clear. 我希望一切都清楚。

 $(function() { "use strict"; var commentsform = $('#commentsForm'); var errorTemplate = '<div class="alert alert-danger"><span class="glyphicon glyphicon-warning-sign"></span>&nbsp;{error}</div>'; var successTemplate = '<div class="alert alert-success"><span class="glyphicon glyphicon-warning-sign"></span>&nbsp;{error}</div>'; var commentsTemplate = '<div class="row" id="comments"><div class="col-md-2 col-sm-2 hidden-xs"><figure class="thumbnail">{photo}<figcaption class="text-center">{username}</figcaption></figure></div><div class="col-md-10 col-sm-10"><div class="panel panel-default arrow left"><div class="panel-body"><header class="text-left"><div class="comment-user"><i class="fa fa-user"></i> {username}</div><time class="comment-date"datetime="{date}"><i class="fa fa-clock-o"></i> {date}</time></header><div class="comment-post">{comment}</div></div></div></div></div>'; var messages = $('#messages'); var comments = $(''); //witch one selector i have to select for it? commentsform.submit(function() { $.ajax({ dataType: "json", url: '/users/ajax/add-comments/', type: 'POST', data: commentsform.serialize(), success: function(response) { if (response) { if (response.errors) { errorTemplate = errorTemplate.replace(/\\{error\\}/, response.msg); messages.show(); messages.html(errorTemplate); setTimeout(function() { messages.slideUp(1500); }, 1000); return false; } else { $('#commentsForm')[0].reset(); var comment = commentsTemplate; successTemplate = successTemplate.replace(/\\{error\\}/, response.msg); messages.show(); messages.html(successTemplate); setTimeout(function() { messages.slideUp(1500); }, 1000); comment = comment.replace(/\\{photo\\}/, response.avatar); comment = comment.replace(/\\{username\\}/, response.username); comment = comment.replace(/\\{comment\\}/, response.comment); comment = comment.replace(/\\{date\\}/, response.date); comments.append(comment); comments.find('#comments ::after').slideDown(); } } } }); return false; }); }); 
 <section class="comment-list"> <div class="row comments"> <div class="col-md-10 col-sm-10"> <div class="panel panel-default arrow left"> <div class="panel-body"> <header class="text-left"> <div class="comment-user"> <i class="fa fa-user"></i> </div> <time class="comment-date" datetime=""> <i class="fa fa-clock-o"></i> </time> </header> <div class="comment-post"></div> </div> </div> </div> </div> </section> 

I don't know how fix that! 我不知道该如何解决!

Add any of the following as the last statement in the success callback of ajax . 将以下任何内容添加为ajaxsuccess回调中的最后一条语句。

Use last() 使用last()

$('.comments').last().slideDown();

OR 要么

You can also use :last pseudo-selector: 您也可以使用:last伪选择器:

$('.comments:last').slideDown();

Both of these will give you the last element having comments class 这两个都将为您提供带有comments类的最后一个元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM