Jquery wp_ajax example

Published
2023-01-10
浏览次数 :  151

jQuery(document).on("click", "#fa-loadmore", function ($) {
    var _self = jQuery(this), 
    _postlistWrap = jQuery('.posts-con'), 
    _button = jQuery('#fa-loadmore'), 
    _data = _self.data(); 
    if (_self.hasClass('is-loading')) { return false } else {
        _button.html('<i class="icon-spin6 animate-spin"></i> 加载中...'); 
        _self.addClass('is-loading'); 
        jQuery.ajax({
            url: '/wp-admin/admin-ajax.php', 
            data: _data, 
            type: 'post', 
            dataType: 'json', 
            success: function (data,textStatus,jqXHR) {
              
                if (jqXHR.status == 500) { 
                    _button.data("paged", data.next).html('加载更多'); 
                    createButterbar('服务器正在努力找回自我  o(∩_∩)o') } 

                else if (jqXHR.status == 200) {
                    console.log(data.code);
                    _postlistWrap.append(data.code); 
                    
                    if (jQuery.isFunction(jQuery.fn.lazyload)) { 
                        jQuery("img.lazy,img.avatar").lazyload({ effect: "fadeIn", }); }
                    if (data.next && data.next <= _data['total'] ) {
                        console.log(data.next);
                        if (suxingme_url.wow) { 
                            var btn = new WOW({ boxClass: 'button-more', animateClass: 'animated', offset: 0, mobile: true, live: true }); 
                            btn.init(); 
                        }
                        _button.data("paged", data.next).html('加载更多');
                    } else { _button.hide() }
                }

                _self.removeClass('is-loading')
            }, 
            error: function (data) { console.log(data.responseText); console.log(data); }
        })
    }
}); 

  • 标签1
  • 标签1
  • 标签1
Top