简体   繁体   English

无法在jQuery中删除类

[英]Can not removeClass in jQuery

The loading class is added but it doesn't removed. 加载类已添加,但未删除。 How to fix this ? 如何解决呢?

$(".phones").addClass("loading");
    that = this
    setTimeout(function() {
        $(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 ,如果没有更小的封闭范围,则指向window对象。

that=this has nothing to do with $(".phones") as your indentation hints at. that = this与$(“。phones”)无关,因为您的缩进提示。

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 . 这里的问题是,您要添加的类一组由类选择定义的元素.phones但你从保存的项目定义的单个元素中取出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); that = this更改为that = $(this); , Not tested but believe this would help ,未经测试,但相信这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM