2013-03-07 65 views
0

所以我有一个toggleClass来改变一些div的类(它改变背景),当一个div被按下时。 当另一个div被按下时,我需要bg变回正常状态,并关闭".subtitle",并切换您按下的新div。jQuery toggleClass - 返回普通类?

现在,关闭和打开功能工作正常,但背景没有改变。

标准类是.title,我也有一个.title.close其中只包含其他背景。

我可以在这里得到一些帮助吗?

CSS: 
.title: contains the normal bg 
.title.open: contains the "open" bg 
.title.close: contains the bg of title(the normal bg) 

jQuery 
$(document).ready(function() 
{ 
    $(".title").click(function() 
    { 
     var $element = $(this); 

     //What I've tried 
     $('open').toggleClass(''); 
     $('.subtitle').hide(500); 

//This is changing the bg, and show the ".subtitle" you've pressed 
     $element.toggleClass('open'); 
     $element.children('.subtitle').toggle(500); 
    }); 
}); 
+0

这是正确的选择:'$('open')。toggleClass('');'?或者你应该使用$('。open')。 – 2013-03-07 09:14:32

回答

3

当你不想切换,但具体要添加或删除一个类,无论目前的状态,你可以只使用.addClass().removeClass()甚至清除所有类。一些例子:

// remove the "open" class from a particular element 
$element.removeClass("open"); 

// remove the "open" class from all elements that have it 
$(".open").removeClass("open"); 
+0

哦,上帝..确实尝试过,一定是做了一件非常错误的事情,因为它现在像一种魅力一样工作......谢谢! – 2013-03-07 09:14:41