简体   繁体   English

jQuery:仅在单击“加载更多…”时才加载下一页

[英]Jquery: only loads the next page on first click of “load more…”

I need to load contents on each click of the text "load more" at the end if the DIV. 如果需要DIV,我需要在每次单击文本“加载更多”时加载内容。 The code I have currently is as below: 我目前拥有的代码如下:

<div id="Container"> <p class="load">load more</P> </div>

and

$(".load:last").click(function(){
$(".load:last").load("nextPage.php", function() {
$(this).hide();

$("div#Container").append($(this).html());
$("div#Container").append("<p class=\"load\">load more...</p>");

});

The first click works as it loads more from nextPage.php. 第一次单击有效,因为它可以从nextPage.php中加载更多内容。 However the consequent click doesn't trigger the load. 但是,随之而来的点击不会触发负载。 What am I doing wrong? 我究竟做错了什么?

    $(".load:last").live('click', function () {
        $(this).remove();
        //Or
        // $(this).hide();
        $(".load:last").load("nextPage.php", function () {
            $("div#Container").append($(this).html());
            $("div#Container").append("<p class=\"load\">load more...</p>");
        });
    });

As the .load append every time to the div#Container as new, you should use a live event handler to that .load like following: 由于每次将.load作为新追加到div#Container ,您应使用实时事件处理程序对该.load如下操作:

$("div#Container").on('click', '.load', function() {
  $(this).parent('div#Container').load('nextPage.php', function() {
     $('div#Container').append("<p class=\"load\">load more...</p>");
  })
})

Try using .live() method or.on() of jquery , this attaches an event handler for all elements which match the current selector, now and in the future. 尝试使用jquery的.live()方法或.on() ,这将为现在和将来与当前选择器匹配的所有元素附加事件处理程序。

Click here for information and example for .live event in jquery 单击此处以获取有关jQuery中.live事件的信息和示例
I hope this will definitely solve you problem. 我希望这一定能解决您的问题。

Best of Luck! 祝你好运! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM