通过Ajax加载HTML/jQuery的问题

通过Ajax加载HTML/jQuery的问题,jquery,html,ajax,Jquery,Html,Ajax,以下是我网站正文中的一些简单HTML行: HTML <h1 class="trigger"> First Headline </h1> <div class="toggle_container"> Some Lorem Ipsum here </div> <h1 class="trigger"> First Headline </h1> <div class="toggle_containe

以下是我网站正文中的一些简单HTML行:

HTML

<h1 class="trigger">
    First Headline
</h1>
<div class="toggle_container">
    Some Lorem Ipsum here
</div>
<h1 class="trigger">
     First Headline
</h1>
<div class="toggle_container" style="display: none;">
     Some Lorem Ipsum here
</div>
加载站点后,HTML将更改为该页面,并单击标题进行切换 “Lorem ipsum”容器。到目前为止,一切正常

HTML

<h1 class="trigger">
    First Headline
</h1>
<div class="toggle_container">
    Some Lorem Ipsum here
</div>
<h1 class="trigger">
     First Headline
</h1>
<div class="toggle_container" style="display: none;">
     Some Lorem Ipsum here
</div>

第一个标题
这里有一些益智
现在我的问题来了:有一个 “加载更多帖子”按钮位于站点底部,它使用AJAX添加了一个新的HTML块

此HTML块正确显示,但不知何故被忽略 上面的jQuery

我做错了什么? 我是否必须将jQuery添加到AJAX中? 如果是,具体在哪里

下面是AJAX代码。评论是由剧本的作者,而不是我

Ajax/jQuery

$(document).ready( function() {
    $('.trigger').not('.trigger_active').next('.toggle_container').hide();
    $('.trigger').click( function() {
        var trig = $(this);
        if ( trig.hasClass('trigger_active') ) {
            trig.next('.toggle_container').slideToggle('slow');
            trig.removeClass('trigger_active');
        } else {
            $('.trigger_active').next('.toggle_container').slideToggle('slow');
            $('.trigger_active').removeClass('trigger_active');
            trig.next('.toggle_container').slideToggle('slow');
            trig.addClass('trigger_active');
        };
        return false;
    });
});
jQuery(document).ready(function($) {

    // The number of the next page to load (/page/x/).
    var pageNum = parseInt(pbd_alp.startPage) + 1;

    // The maximum number of pages the current query can return.
    var max = parseInt(pbd_alp.maxPages);

    // The link of the next page of posts.
    var nextLink = pbd_alp.nextLink;

    /**
     * Replace the traditional navigation with our own,
     * but only if there is at least one page of new posts to load.
     */
    if(pageNum <= max) {
        // Insert the "More Posts" link.
        $('#content')
            .append('<div class="pbd-alp-placeholder-'+ pageNum +'"></div>')
            .append('<p id="pbd-alp-load-posts"><a href="#">↓ Archive</a></p>');

        // Remove the traditional navigation.
        $('.navigation').remove();
    }


    /**
     * Load new posts when the link is clicked.
     */
    $('#pbd-alp-load-posts a').click(function() {



        // Are there more posts to load?
        if(pageNum <= max) {

            // Show that we're working.
            $(this).text('...');

            $('.pbd-alp-placeholder-'+ pageNum).load(nextLink + ' .post',
                function() {
                    // Update page number and nextLink.
                    pageNum++;
                    nextLink = nextLink.replace(/\/page\/[0-9]?/, '/page/'+ pageNum);

                    // Add a new placeholder, for when user clicks again.
                    $('#pbd-alp-load-posts')
                        .before('<div class="pbd-alp-placeholder-'+ pageNum +'"></div>')

                    // Update the button message.
                    if(pageNum <= max) {
                        $('#pbd-alp-load-posts a').text('Older entries');
                    } else {
                        $('#pbd-alp-load-posts a').text('All items loaded.');
                    }
                }
            );
        } else {
            $('#pbd-alp-load-posts a').append('.');
        }   

        return false;
    });
});
jQuery(文档).ready(函数($){
//要加载的下一页的编号(/page/x/)。
var pageNum=parseInt(pbd_alp.startPage)+1;
//当前查询可以返回的最大页数。
var max=parseInt(pbd_alp.maxPages);
//下一页文章的链接。
var nextLink=pbd_alp.nextLink;
/**
*用我们自己的导航取代传统的导航,
*但前提是至少要加载一页新帖子。
*/

如果(pageNum我看到的问题是绑定事件。按照您绑定它的方式,它只绑定到标记中当前可用的元素

通过ajax获取html添加的新元素不会添加到事件处理程序中。为此,您需要将事件绑定语句修改为委托绑定:

$(document).on('click', '.trigger', function() {

});

啊,好的。非常感谢,但我不明白这个代表团的内容是什么。任何更聪明/更简单的解决方案?都会很有帮助。你读过这篇文章吗?你不懂什么?我不喜欢编程和计算机,我想这是我的主要问题。我读过这篇文章,就像读中文报纸,我想我只需要some是时候了解它的全部内容了:使用术语“live”是不正确的,并且对现在删除的jQuery方法的引用过于强烈。我用正确的术语编辑了你的答案(希望你不介意)。谢谢,这似乎是有意义的。我会尝试一下,明天再检查。。。