奇怪的问题,jQuery .removeClass()没有做任何事情 [英] Odd issue with jQuery .removeClass() not doing anything

查看:352
本文介绍了奇怪的问题,jQuery .removeClass()没有做任何事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码添加类到一个元素,然后尝试删除它们,并添加不同的1秒后。我得到一些非常奇怪的行为,我甚至不能在一个简单的中看到。






编辑2 :已确认为

尝试使用 .removeAttr('class')而不是 .removeClass()



演示:



http://jsfiddle.net/MvvmJ/8/



希望这有助于我们,如果您有任何问题,请告诉我们!


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.

Here's the relevant JavaScript code I have:

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.

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?

Additional info: jQuery latest (v.1.9.0 I think), 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.


Edit 2: This was confirmed as a bug in jQuery, and has been fixed.

解决方案

Try using .removeAttr('class') rather than .removeClass().

DEMO:

http://jsfiddle.net/MvvmJ/8/

Hope this helps and let me know if you have any questions!

这篇关于奇怪的问题,jQuery .removeClass()没有做任何事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆