jQuery .removeClass() 没有做任何事情的奇怪问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14532327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Odd issue with jQuery .removeClass() not doing anything
提问by jbabey
I have some code that adds classes to an element and then tries to remove them and add different ones 1 second later. I'm getting some very odd behavior that I can't even reproduce in a simple jsfiddle example.
我有一些代码将类添加到元素,然后尝试删除它们并在 1 秒后添加不同的类。我得到了一些非常奇怪的行为,我什至无法在一个简单的jsfiddle 示例中重现。
Here's the relevant JavaScript code I have:
这是我拥有的相关 JavaScript 代码:
console.log('before destroyed: ' + currentTile.get(0).className);
currentTile.addClass(classes.destroyed);
console.log('after destroyed: ' + currentTile.get(0).className);
setTimeout(function () {
console.log('before blanking: ' + currentTile.get(0).className);
currentTile.removeClass().addClass(classes.blank + ' ui-draggable');
console.log('after blanking: ' + currentTile.get(0).className);
}, 2000);
And here is what the console is saying:
这是控制台所说的:
As you can see, adding the destroyed
class works fine, but the call to removeClass()
inside of the setTimeout
appears to be doing nothing, and then the .addClass(classes.blank + ' ui-draggable');
also appears to be working fine. Also, if I pass a single class to removeClass
it removes that one class without a problem.
如您所见,添加destroyed
类工作正常,但对removeClass()
内部的调用setTimeout
似乎没有任何作用,然后.addClass(classes.blank + ' ui-draggable');
似乎也工作正常。此外,如果我将单个类传递给removeClass
它,则可以毫无问题地删除该类。
If it were an issue of context or currentTile
being the wrong element, I would think the addClass
would also fail? Anyone have any idea what is going on here?
如果是上下文问题或者currentTile
是错误的元素,我认为addClass
这也会失败?任何人都知道这里发生了什么?
Additional info: jQuery latest (v.1.9.0 I think), jQuery UI v 1.10.0, Chrome v.24.0.1312.56 m
附加信息:jQuery 最新版(我认为是 v.1.9.0)、jQuery UI v 1.10.0、Chrome v.24.0.1312.56 m
Edit: The problem appears to be directly related to jQuery UI, and can be seen happening in this fiddle.
编辑:问题似乎与 jQuery UI 直接相关,并且可以在这个 fiddle 中看到。
Edit 2: This was confirmed as a bug in jQuery, and has been fixed.
采纳答案by Dom
Try using .removeAttr('class')
rather than .removeClass()
.
尝试使用.removeAttr('class')
而不是.removeClass()
.
DEMO:
演示:
Hope this helps and let me know if you have any questions!
希望这会有所帮助,如果您有任何问题,请告诉我!