基本上,我试图创建一个像新闻源一样的facebook / twitter,我只在数据库中显示至少3个最新帖子。
帖子底部的,我有load more posts..
点击div后,当然会显示另外3个旧帖子等等..
目前,我的数据库中有15个帖子,显示如下(最近添加的帖子为id 15)
id:15 - some posts - some comments
id:14 - some posts - some comments
id:13 - some posts - some comments
load more...
上面的会显示每篇帖子的帖子和相关的评论。
<ul id="post">
<?php while.. { ?>
<li><?php echo id; ?> - <?php echo posts; ?> </li>
<ul id="comments">
<?php while .. {?>
<li> <?php echo comments; ?> </li>
<?php } //end while ?>
</ul>
<?php } //end of while?>
</ul>
<span last_id="<?php echo id?>"> Load More ... </span>
您还可以看到我的<span>
标记,该标记显示结果的最后一个ID(13)。然后由我的jQuery处理。
这是<span>
标记的jQuery,当点击它时,它将获得最后一个帖子ID(13),并使用该最后一个ID值来获取另一组3个旧帖子12
,11
,10
$(function() {
$('.loadmore').click(function () {
var last_id = $(this).attr('last_id');
$.ajax({
type:"POST",
url:"../portal/includes/get_posts.php",
data: "last_id="+last_id,
success: function(html) {
$('#post').append(html);
}
});
return false;
});
});
获取的值将附加到我的<ul id="post">
,从而产生此格式
id:15 - some posts - some comments
id:14 - some posts - some comments
id:13 - some posts - some comments
id:12 - some posts - some comments
id:11 - some posts - some comments
id:10 - some posts - some comments
load more...
但是当我再次点击load more...
时,它不会查询另外一组3个旧帖子。
看来,我的jQuery无法识别附加的内容。
我该如何解决这个问题?
感谢。
答案 0 :(得分:0)
你应该有一个班级:
<span class="loadmore" data-last_id="<?php echo id?>">
正如已经指出的那样,应该在jQuery中使用:
$(document).on('click', '.loadmore', function () {
//
});
$('.post').append(html);
应为$('#post').append(html);
我还会考虑将last-id
属性更改为更友好的数据属性:
<span data-last_id="<?php echo id?>">
将使用jQuery获取:
var last_id = $(this).data('last_id');
答案 1 :(得分:0)
事件可能存在问题,如果要使用加载了ajax的内容进行操作,则需要使用事件绑定。所以,改变这个:
$('.loadmore').click(function () {
对此:
$(document).on('click', '.loadmore', function () {