簡體   English   中英

jQuery 加載更多按鈕

[英]jQuery load more button

我有一小段很棒的代碼,可以很好地與 Wordpress 配合使用。 它基本上為自定義循環添加了一個加載更多按鈕,並在單擊時添加下面的下一批帖子。

我的問題是從下面的代碼片段中獲取的這行代碼: error: function() { jQuery('button.load-more').attr('disabled', 'disabled').text('No More Posts') } // Disable the button and change its contents when there are no more posts

基本上當沒有更多帖子時,按鈕不會按照指示禁用。 有人可以幫我解決嗎? 這是完整的JS:

var page = 1;

loadMore = function () {
    page++ //Increments the page number in the request
    $.ajax({
        url: './page/' + page,
        beforeSend: function () {
            $('#wait').show().parent().find('button.load-more').hide();
        },
        complete: function () {
            $('#wait').hide().parent().find('button.load-more').show();
        },
        success: function (data) {
            var $data = $(data).find('article');
            // Replace div#the-posts with the name of your post container
            jQuery('#posts .inner').append($data);
        },
        error: function () {
            jQuery('button.load-more').attr('disabled', 'disabled').text('No More Posts')
        } // Disable the button and change its contents when there arew no more posts

    }); // End Ajax
}; // End loadMore

jQuery('button.load-more').on('click', loadMore);

錯誤函數僅在服務器返回某種錯誤狀態代碼時調用,這似乎不會發生。

我的猜測是您需要檢查響應。 以下可能有效:

    success: function (data) {
        var $data = $(data).find('article');
        if ($data.length > 0) {  
          // Replace div#the-posts with the name of your post container
          jQuery('#posts .inner').append($data);
        }
        else {
          jQuery('button.load-more').attr('disabled', 'disabled').text('No More Posts');
        }
    },

但這只是一個猜測,因為我不知道響應的樣子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM