来自AJAX结果的附加数据不会执行另一个jQuery函数[重复](appended data from an AJAX result wont execute another jQuery funct

编程入门 行业动态 更新时间:2024-10-25 00:36:11
来自AJAX结果的附加数据不会执行另一个jQuery函数[重复](appended data from an AJAX result wont execute another jQuery function [duplicate])

这个问题在这里已有答案:

在通过AJAX 6答案 加载内容后,jQuery不起作用

基本上,我试图创建一个像新闻源一样的facebook / twitter,我只在数据库中显示至少3个最新帖子。

在帖子的底部,我有一个可点击的div 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

$(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无法识别附加内容。

我怎么解决这个?

谢谢。

This question already has an answer here:

jQuery doesn't work after content is loaded via AJAX 7 answers

basically, im trying to create a facebook/twitter like news feed, where i display a minimum of 3 latest post only from the database.

on the bottom of the post, i have a clickable div for load more posts..

upon clicking the div, of course it displays the another 3 old posts and so on..

currently, i have 15 posts in my database, it is displayed as followed (id 15 as the recently added post)

id:15 - some posts - some comments id:14 - some posts - some comments id:13 - some posts - some comments load more...

above will display the posts and the associated comments per posts.

<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>

you can also see my <span> tag, which displays the last ID (13) of the result. which will then be handled by my jQuery below.

this the jQuery for the <span> tag, when this is clicked, it will get the last post id (13), and use that last ID value to get another set of 3 old post 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; }); });

the fetched value will append to my <ul id="post"> resulting to this format

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...

but when i click the load more... again, it wont query for another set of 3 old posts.

it appears, that my jQuery cannot recognize the appended.

how can i resolve this?

thanks.

最满意答案

事件可能存在问题,如果要使用ajax加载的内容进行操作,则需要使用事件绑定。 所以,改变这个: $('.loadmore').click(function () { 对此: $(document).on('click', '.loadmore', function () {

There may be a problem with event, you need to use event binding, if you want to manipulate with ajax loaded content. So, change this: $('.loadmore').click(function () { to this: $(document).on('click', '.loadmore', function () {

更多推荐

本文发布于:2023-08-07 00:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458804.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   数据   appended   AJAX   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!