所以我有一个将数据附加到 php 页面的 ajax 函数。它就像一个评论面板,当您滚动到底部时,您可以单击一个按钮并加载更多评论。这是加载更多评论的 ajax 函数。
$("#loadmorecomments").click(function(){
$('div#loadmorecomments').show();
.ajax({
url: "loadmorecomments.php?lastComment=" + $(".post:last").attr("id") + "&sname=<?php echo $link ?>",
success: function(html){
if(html){
$("#postspace").append(html);
$("div#loadmorecomments").hide();
else{
("div#loadmorecomments").replaceWith("div#done");
}
});
});
这是它附加到原始 php 页面的页面。包括 javacript 在内的所有这些代码都是 loadmorecomments.php 的一部分,它被附加到原始页面。所以在所有这些完全相同的代码工作的原始页面上。不适用于附加的这些新评论。
if($stmt = mysqli_prepare($mysqli, "SELECT post.text,post.username,post.id,post.likes,post.dislikes,post.image,users.avatar FROM post LEFT JOIN users ON post.username = users.username WHERE post.school='$sname' AND post.id < $lastcomment ORDER BY post.id DESC LIMIT 0,10")){
mysqli_stmt_execute($stmt);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt,$comments,$user, $id,$likes,$dislikes,$image,$avatar);
$counter = 0;
while(mysqli_stmt_fetch($stmt)){
/////////////////Get Image///////////////////
switch($user){
case "Anonymous":
$img = "design/anonymous.png";
break;
case $user != "Anonymous" && !empty($avatar):
$img = $avatar;
break;
case $user != "Anonymous" && empty($avatar):
$img = "logo.png";
break;
}
if(!empty($image) || $image != "images/"){
$comimage = $image;
}else{
$comimage = "";
}
echo "<div class = 'post' id = '$id'>";
echo "<a href = './visitpage.php?userpage='$user'>$user<br /><img src='$img'/></a>";
if(!empty($comimage)){
echo "<img src='$comimage' class='comimage'></img>
<h3>$comments</h3>
";
}else{
echo "<p>$comments</p>";
}
echo "<div style='border: 2px solid #000; height: 40px; margin-right: 20px; margin-left:90px; background: #F1F2F6'class='commentpanel' id='commentpanel_$id'>";
if(in_array($id, $likedcomments)){
echo "<img src='design/liked.png' class='like' /><h4 class='liketext' id='liketext_$id'>You and $likes</h4>";
}else{
echo " <a href='' id='like_$id'><img src='design/like.png' class='like' /></a><h4 class='liketext' id='liketext_$id'>$likes</h4>";
}
if(in_array($id, $dislikedcomments)){
echo "<img src='design/disliked.png' class='like' /><h4 class='liketext' id='liketext_$id'>You + $dislikes</h4>";
}else{
echo " <a href='' id='dislike_$id'><img src='design/dislike.png' class='like' /></a><h4 class='liketext' id='liketext_$id'>$dislikes</h4>";
}
$link = urlencode($sname);
echo "
<a href= '' id ='toggle_$id'><img src='design/reply.png' class='like'/><h4 class='liketext'>Send Message</h4></a>
<a href= './quote.php?text=$comments&school=$link' ><img src='design/quote.png' class='like'/><h4 class='liketext'>Quote Post</h4></a>
</div>
<div class = 'replypost' id='replypost_$id'>
<form action='' method='POST'>
<input type= 'hidden' name='user' id='user' value='$user' />
<input type= 'hidden' name='comment' id='comment' value='$comments' />
<input type='textarea' name='reply' id= 'reply' class='replycomment' />
<input type= 'submit' id='submit' name='submit' value=' Reply ' class='replybutton' />
</form>
</div>
<br/><br/><br/>
</div>";
$counter++;
}
mysqli_stmt_close($stmt);
}
mysqli_close($mysqli);
}
?>
<script><!---- Load More Comments !---->
$("#loadmorecomments").click(function(){
$('div#loadmorecomments').show();
$.ajax({
url: "loadmorecomments.php?lastComment=" + $(".post:last").attr("id") + "&sname=<?php echo $link ?>",
success: function(html){
if(html){
$("#postspace").append(html);
$("div#loadmorecomments").hide();
}else{
$("div#loadmorecomments").replaceWith("div#done");
}
}
});
});
$("a[id ^= 'toggle']").click(function(event){
event.preventDefault();
$("div [id='replypost_"+$(this).attr('id').replace('toggle_','')+"']").toggle();
});
var likecounter = 0;
var dislikecounter = 0;
$("a[id ^= 'like_']").click(function(event){
event.preventDefault();
var value = $(this).attr("id").replace('like_','');
$.ajax({
url: "./like.php?id="+value+"&sname=<?php echo $link ?>",
success: function(html){
if(html && likecounter == 0 && dislikecounter == 0 ){
$("#commentpanel_"+value).append("<br/><br/><br /><br /><h4>You Like This Post</h4>");
likecounter = 1;
}else{
$("div#loadmorecomments").replaceWith("div#done");
}
}
});
});
$("a[id ^= 'dislike_']").click(function(event){
event.preventDefault();
var value = $(this).attr("id").replace('dislike_','');
$.ajax({
url: "./dislike.php?id="+value+"&sname=<?php echo $link ?>",
success: function(html){
if(html && likecounter == 0 && dislikecounter == 0 ){
$("#commentpanel_"+value).append("<br/><br/><br /><h4>You Hate This Post</h4>");
dislikecounter = 1;
}else{
$("div#loadmorecomments").replaceWith("div#done");
}
}
});
});
</script>
我想知道为什么当我单击应该调用这些 javascript 函数的锚标记时,它只是刷新页面而没有调用任何 javascript 函数。
注意:原始页面的发布评论和调用javascript函数的格式完全相同,并且可以完美运行。唯一不起作用的情况是从调用的 loadmorecomments.php 文件加载注释时。
我知道这是一个棘手的问题,如果我可以进一步澄清,请告诉我谢谢。