我有一小段很棒的代码,可以很好地与 Wordpress 配合使用。它基本上向自定义循环添加了一个“加载更多”按钮,单击时会在下面添加下一批帖子。
我遇到的问题是从下面的代码片段中获取的这行代码: error: function() { jQuery('button.load-more').attr('disabled', 'disabled')。 text('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');
}
},
但这只是一个猜测,因为我不知道响应是什么样的。
关于javascript - jQuery 加载更多按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15430724/