开发者

Can not removeClass in jQuery

开发者 https://www.devze.com 2023-04-07 12:58 出处:网络
The loading class is added but it doesn\'t removed. How to fix this ? $(\".phones\").addClass(\"loading\");

The loading class is added but it doesn't removed. How to fix this ?

$(".phones").addClass("loading");
    that = this
    setTimeout(function() {
    开发者_JAVA技巧    $(that).removeClass('loading');
    }, 3000);


Something like this would work:

var phones = $(".phones").addClass("loading");
setTimeout(function() {
    phones.removeClass('loading');
}, 3000);

I'm not sure why you're worried about scope, though.


Are you trying to do:

$(".phones").addClass("loading");
setTimeout(function() {
    $(".phones").removeClass('loading');
}, 3000);

that = this, which refers to the window object if there is no smaller enclosing scope.


that=this has nothing to do with $(".phones") as your indentation hints at.

var phones = $(".phones").addClass("loading");
setTimeout(function() {
    phones.removeClass('loading');
}, 3000);


The problem here is that you are adding the class to a group of elements defined by the class selector .phones but your removing it from a single element defined by the saved item that. It seems like you want to add and remove from the same group. To do this just use the same queries

$(".phones").addClass("loading");    
setTimeout(function() {
  $(".phones").removeClass('loading');
}, 3000);


Change that = this to that = $(this);, Not tested but believe this would help

0

精彩评论

暂无评论...
验证码 换一张
取 消