Closed. This question needs details or clarity。它当前不接受答案。
                        
                    
                
            
        
            
        
                
                    
                
            
                
                    想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                
                    3年前关闭。
            
        

    

我用jquery创建了一个分页系统,该脚本运行得很好,但是当我更改页面时,由于刷新,该脚本使我回到了原始页面。

分页如何使刷新工作?

谢谢

$(document).ready(function() {
    setInterval(function(){
    $("#results" ).load( "recherche.php");
    $("#results").on( "click", ".pagination a", function (e){
        e.preventDefault();
        $(".loading-div").show();
        var page = $(this).attr("data-page");
        $("#results").load("recherche.php",{"page":page}, function(){
            $(".loading-div").hide();

            });

        });

    }, 1000);
});

<div id="results">
<div class="loading-div">
<img src="img/loader.gif">
</div>
</div>

最佳答案

感谢您在评论中的其他详细信息。我相信您的问题是“ setInterval” ...您可能只希望在页面加载时仅运行一次此代码-然后ajax调用将接管。

在这种情况下,您需要使用setTimeout-setInterval将每1000毫秒重复一次回调。 (在这种情况下)

$(document).ready(function() {
    setTimeout(function(){
        $("#results" ).load( "recherche.php");
        $("#results").on( "click", ".pagination a", function (e){
            e.preventDefault();
            $(".loading-div").show();
            var page = $(this).attr("data-page");
            $("#results").load("recherche.php",{"page":page}, function(){
                $(".loading-div").hide();

            });
        });
    }, 1000);
});

<div id="results">
    <div class="loading-div">
        <img src="img/loader.gif">
    </div>
</div>


根据评论更新

好的,因此,如果您确实想每秒刷新一次页面(这很常见!),那么您将需要记住当前所在的页面,以便以后的调用始终能加载正确的页面。您应该使用一个变量来保存当前页面值,默认为“ page 1”,瞧!

$(document).ready(function() {
    var currentPage = 1;
    var loadPage = function(page) {
        $(".loading-div").show();

        page = page || currentPage; // if no page parameter provided, simply reload the currentPage

        $("#results").load("recherche.php",{"page":page}, function(){
            currentPage = page; // once the page is loaded, update our currentPage value since this is the page we are now looking at

            $(".loading-div").hide();
        });
    };

    // Move this out of the interval - you probably don't want to set-up a click handler every time your interval is called!
    $("#results").on( "click", ".pagination a", function (e){
        e.preventDefault();
        loadPage($(this).attr("data-page"));
    });

    setInterval(function(){
        loadPage(); // every 1 second, reload the current page
    }, 1000);

    loadPage(1); // initial loading of the first page
});

<div class="loading-div">
    <img src="img/loader.gif">
</div>
<div id="results">
</div>


最后需要注意的一件事:第一次加载内容时,它将覆盖“结果” div的内容-包括您的loader.gif ...我已将加载器移至结果div之外,以便可以重复使用每次您请求一个新页面时...

希望这可以帮助!

10-05 20:54
查看更多