That is, when you click to load more, the,. RemoveClass () fails and doesn't work. What's wrong?

/ / Page load
jQuery (document) .ready (function ($) {

    var loading=false
    $("div-sharppost-read-more a").click( function() {
        if(loading)return
        loading=true
        $this = $(this);
        $this.removeClass().addClass("loading"); //aloadingclass
        var href = $this.attr("href"); //
        if (href != undefined) { //
            $.ajax( { //ajax
                url: href, //
                type: "get", //get
                error: function(request) {
                    loading=false
                },
                success: function(data) { //
                    loading=false
                    $this.removeClass("loading"); //loading
                    var $res = $(data).find(".list"); //
                    $(".box").append($res); //posts-loop
                    var newhref = $(data).find("-sharppost-read-more a").attr("href"); //
                    if( newhref != undefined ){
                        $("-sharppost-read-more a").attr("href",newhref);
                        
                    }else{
                        $("-sharppost-read-more a").html("").removeAttr("href");
                        
                    }
                }
    });   
}   
return false;   

});

});

that is, when I click to load more, I want to delete the style of the button and then directly adopt the style of loading.,. RemoveClass () seems to be invalid and doesn"t work.
that is, when you click to load more, the new button style of loading will be displayed directly. How to do it?

Apr.25,2021
The this in

ajax is not what you mean, but this refers to the current success callback function. Replace


with $(that) in var that = this,ajax before requesting ajax

$this.removeClass (). AddClass ('loading'); won't this code report an error?

The use of

http://www.w3school.com.cn/jq.
toggleClass method, combined with what was said above, selects this element with that, and then passes it to success. This element calls the toggleClass method to switch between the two types of styles

.

agrees that upstairs, you need to assign this to a local variable


this points to confusion. You can think that who called the this in ajax? Is that your element calling it? And $this is written in the wrong way, isn't it? Add parentheses to this

Menu