简体   繁体   English

Ajax加载更多内容

[英]Ajax Load more content

I want to load some content from /page-0.html?to-page=2 and my current page is /page-0.html?to-page=1. 我想从/page-0.html?to-page=2加载一些内容,而我当前的页面是/page-0.html?to-page=1.

Here is my code: 这是我的代码:

    $(document).ready(function() {

    var page = 1;

    $.get('/page-0.html?to-page=' +page, function(html) {
    if(html) {

    $('#result').append(html);
    $('#more').text('Load More Post'); //add text "Load More Post" to button again
        } else {
            $('#more').text('No more Post to load'); // when last record add text "No more posts to load" to button.
        }
    $('#more').click(function() {
    $('#more').html('<img src="ajax-loader.gif"');
    page += 1;
    });
    }, 'json');

    });

html: HTML:

    <div id="result"></div> <button id="more">Load more</button>

If i click load more button it should change the url /page-0.html?to-page=1 to 2. Is it possible? 如果我单击“加载更多”按钮,则应将url /page-0.html?to-page=1 to 2.更改/page-0.html?to-page=1 to 2.可以吗?

I'm new to jquery. 我是jquery新手。 Can someone help me to find my mistake here? 有人可以帮我在这里找到我的错误吗?

Try this: 尝试这个:

$(document).ready(function() {

    var page = 1;
    var getData;
    (getData = function() {
        $.get('/page-0.html?to-page=' +page, function(html) {
            if(html) {

                $('#result').append(html);
                $('#more').text('Load More Post'); //add text "Load More Post" to button again
            } else {
                $('#more').text('No more Post to load'); // when last record add text "No more posts to load" to button.
            }
        }, 'json');
    })();
    $('#more').click(function() {
        $('#more').html('<img src="ajax-loader.gif"');
        page += 1;
        getData();
    });
});

I have used getData function as an IIFE so that request is automatically hit for the first time and then called the same function later on clicking more. 我已将getData函数用作IIFE,以便第一次自动命中请求,然后在单击更多按钮时调用该函数。 HTH HTH

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

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