Javascript 带分页的Jquery自动刷新

Javascript 带分页的Jquery自动刷新,javascript,php,jquery,pagination,Javascript,Php,Jquery,Pagination,我用jquery创建了一个分页系统,脚本运行得很好,但是当我更改页面时,由于刷新,脚本使我返回到原始页面 如何使刷新与分页一起工作 多谢各位 $(document).ready(function() { setInterval(function(){ $("#results" ).load( "recherche.php"); $("#results").on( "click", ".pagination a", function (e){ e.prev

我用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>
$(文档).ready(函数(){
setInterval(函数(){
$(“#results”).load(“recherche.php”);
$(“#结果”)。在(“单击”,“分页a”,函数(e){
e、 预防默认值();
$(“.loading div”).show();
var page=$(this.attr(“数据页”);
$(“#results”).load(“recherche.php”,{“page”:page},function(){
$(“.loading div”).hide();
});
});
}, 1000);
});

感谢您在评论中提供更多详细信息。我相信你的问题是“设定间隔”。。。您可能只想在页面加载时运行这段代码一次,然后ajax调用就会接管

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

$(文档).ready(函数(){
setTimeout(函数(){
$(“#results”).load(“recherche.php”);
$(“#结果”)。在(“单击”,“分页a”,函数(e){
e、 预防默认值();
$(“.loading div”).show();
var page=$(this.attr(“数据页”);
$(“#results”).load(“recherche.php”,{“page”:page},function(){
$(“.loading div”).hide();
});
});
}, 1000);
});
根据评论进行更新

好吧,如果你真的想每秒刷新一次你的页面,(这是很常见的!),那么你需要记住你当前所在的页面,以便以后的调用总是加载正确的页面。您应该使用一个变量来保存当前页面值,默认为“第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>
$(文档).ready(函数(){
var currentPage=1;
var loadPage=函数(第页){
$(“.loading div”).show();
page=page | | currentPage;//如果没有提供页面参数,只需重新加载当前页面即可
$(“#results”).load(“recherche.php”,{“page”:page},function(){
currentPage=page;//加载页面后,更新currentPage值,因为这是我们现在查看的页面
$(“.loading div”).hide();
});
};
//将其移出间隔-您可能不希望每次调用间隔时都设置单击处理程序!
$(“#结果”)。在(“单击”,“分页a”,函数(e){
e、 预防默认值();
loadPage($(this.attr(“数据页”));
});
setInterval(函数(){
loadPage();//每隔1秒重新加载当前页面
}, 1000);
loadPage(1);//第一页的初始加载
});
最后要注意的是:第一次加载内容时,它将覆盖“results”div的内容-包括loader.gif。。。我已将加载程序移到您的结果分区之外,以便在您每次请求新页面时都可以重新使用它


希望这有帮助

你所说的“脚本让我因为刷新而回到原始页面”到底是什么意思?你的意思是第2页加载,然后第1页再次加载“在上面”?那么recherche.php返回了什么呢。。。实际上,您必须编写一个分页系统,该系统可以在服务器端的会话中管理自身,也可以将页面保留在url中。在这两种情况下,它都完全重写了这里的内容…@MacPrawn是的,当我想进入下一页(例如:page.php?=Page2)时,我执行刷新“setInterval function()”的脚本会将我带回原始页面(page.php)。谢谢你的回答,我想每1秒刷新一次我的页面。甚至没有一次…超级,我删除了.gif,因为否则它会在每次刷新时显示它。非常好,非常感谢你,你是最棒的:)太棒了。所以如果你没有;(我不介意,如果你把我的答案标为“已接受”,这会帮我解决问题……)谢谢
$(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>