Reverse ordered list for jquery submitted comments
Hey guys I have one more question lol. I am using a script that allows users to submit comments through jquery ajax, however when they are submitted, the submitted comments submit at the bottom of the other comments which are sorted in descending order (newest on top) when the page first loads (due to mysql query). Is there a way to make it submit on top through some sort of sorting javascript function?
function prepare(response) {
var d = new Date();
count++;
d.setTime(response.time*1000);
var mytime = d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();
var string = '<li class="shoutbox-list" id="list-'+count+'">'
+ '<span class="date">'+mytime+'</span>'
+ '<span class="shoutbox-list-nick"><a href="statistics.php?user='+response.user+'">'+response.user+'</a>:</span>'
+ '<span class="msg">'+response.message+'</span>'
+'</li>';
return string;
}
function success(response, status) {
i开发者_JS百科f(status == 'success') {
lastTime = response.time;
$('#daddy-shoutbox-list').append(prepare(response));
$('input[name=message]').attr('value', '').focus();
$('#list-'+count).fadeIn('slow');
timeoutID = setTimeout(refresh, 3000);
}
}
<div id="daddy-shoutbox">
<ol id="daddy-shoutbox-list"></ol>
</div>
Use prepend()
instead of append()
.
$('#daddy-shoutbox-list').prepend(prepare(response));
精彩评论