"/-6ren">
gpt4 book ai didi

PHP/JavaScript 置顶?

转载 作者:行者123 更新时间:2023-11-30 10:44:58 29 4
gpt4 key购买 nike

我有一个用 PHP 和 JavaScript 编写的评论系统,但每次我发表评论时,评论都会出现在底部。如何让它显示在顶部?

这是我的代码:

PHP代码:

<?php
$sql = mysql_query( "SELECT * FROM `comments` WHERE perma_id = '$permalink' ORDER BY `date` DESC" );
$count = mysql_num_rows( $sql );
?>

<h2 class="comments"><?php echo $count; ?> Comments</h2>

<div id="flash"></div>

<ol id="update" class="timeline">

<?php
while( $row = mysql_fetch_assoc( $sql ) ) {
$userSql = mysql_query( "SELECT * FROM `members` WHERE `id` = '$row[userid]'" );
$userRow = mysql_fetch_assoc( $userSql );
$name = $userRow[ 'userid' ];
$comment = $row[ 'comment' ];
$cDate = $row[ 'date' ];
$icon = $userRow[ 'icon' ];
?>
<li class="box">
<div class="left"><a href="<?php echo $site_url . '/wall/' . $name; ?>"><img src="<?php echo $site_url . '/' . $icon; ?>"/></a></div>
<div class="right">
<div class="name"><a href="<?php echo $site_url . '/wall/' . $name; ?>"><?php echo $name;?></a> said...</div>
<div class="date"><?php echo date("M d, Y g:i a", $cDate); ?></div>
<div class="clearfix"></div>
<div class="comm">
<div class="inside">
<?php echo $comment; ?>
</div>
</div>
</div>
</li>
<?php
}
?>

JavaScript 代码:

$(function() {
$('.com_submit').click(function() {
var comment = $("#comment").val();
var user_id = $("#user_id").val();
var perma_id = $("#perma_id").val();
var dataString = { comment: comment, user_id: user_id, perma_id: perma_id };
if(comment=='') {
alert('Please Give Valid Details');
}
else {
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="../images/ajax-loader.gif" align="left" /> Loading Comment');
$.ajax({
type: "POST",
url: "../commentajax.php",
data: dataString,
cache: false,
success: function(html){
$("ol#update").append(html);
$("ol#update li:first").fadeIn("slow");
$("#flash").hide();
}
});
}
return false;
});
});

我有 li:first 但它没有排在顶部

最佳答案

使用.prepend()代替.append()

您的 :first 淡入有效,它已经可见。但请记住,这只是一个 jQuery 选择器。

关于PHP/JavaScript 置顶?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8918061/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com