jQuery .removeClass() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21999016/
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
jQuery .removeClass() not working
提问by Tobias Kuess
I need to remove a CSS class from an text field since I want to change its background color. I need to change the color back later, so I have to add and remove classes to the field, depending on some data.
我需要从文本字段中删除 CSS 类,因为我想更改其背景颜色。稍后我需要将颜色改回来,因此我必须根据某些数据在字段中添加和删除类。
.addClass
is working totally fine. The problem is that the original class is above the added, so the changes are not shown, but the class is added properly. I now try to remove the class which is above the added - but I cant remove any classes from my elements. Why is that? What am I doing wrong? Here's the code:
.addClass
工作正常。问题是原来的类在添加的上面,所以没有显示变化,但是类添加正确。我现在尝试删除高于添加的类 - 但我无法从我的元素中删除任何类。这是为什么?我究竟做错了什么?这是代码:
if(i.stadt=="T") {
$("#stadtInput").removeClass("ui-input-text input");
$("#stadtInput").removeClass("ui-input-search input");
$("#stadtInput").addClass( 'textboxRight' );
} else {
$("#stadtInput").addClass( 'textboxWrong' );
}
If I inspect the elements with Webdev-Tools of browser I can see that the textboxRight
/textboxWrong
class is added, but no class is removed. I already tried to remove all classes by .removeClass()
, but that doesn't work either..
如果我使用浏览器的 Webdev-Tools 检查元素,我可以看到添加了textboxRight
/textboxWrong
类,但没有删除任何类。我已经尝试通过 删除所有类.removeClass()
,但这也不起作用..
采纳答案by Alec Moore
I'm not sure if this will fit your specifications, but the jQuery attr() method will take two arguments and replace all of the classes with the class name your provide. This won't work if you have other classes that you want to keep on your element, but if not try:
我不确定这是否符合您的规范,但 jQuery attr() 方法将采用两个参数并将所有类替换为您提供的类名。如果您有其他要保留在元素上的类,这将不起作用,但如果不尝试:
$("#stadtInput").attr("class", "textboxRight");
回答by Svet
The next few lines works for me
接下来的几行对我有用
var current = $(el).closest(".panel").find(".panel-collapse");
if( $(current).hasClass("in") ) {
setTimeout(function(){///workaround
$(".panel-collapse").removeClass("in");
}, 10);
} else {
$(current).addClass("in");
}
回答by Patrick Mutuku
Had this problem earlier on. Am no expert in jquery yet so I don't know if it behaves like css' specificity. My fix was to remove the class by using higher specificity. e.g
Instead of using $(this).removeClass('acb')
, try using higher specificity like $('#div1').children().first()
早有这个问题。我还不是 jquery 的专家,所以我不知道它的行为是否像 css 的特殊性。我的解决方法是通过使用更高的特异性来删除该类。例如,不要使用$(this).removeClass('acb')
,而是尝试使用更高的特异性,例如$('#div1').children().first()
回答by sebasjimenez10
I had the same issue when working on a rails app with the HAML
templating gem as I included the #
at the very beginning and it is not really needed. When I removed it, it started working.
在使用HAML
模板 gem 处理Rails 应用程序时,我遇到了同样的问题,因为我#
在一开始就包含了它,但实际上并不需要它。当我删除它时,它开始工作。
Hope this helps! :)
希望这可以帮助!:)