2014-02-19 72 views
0

我有一個有多個類的元素。我想在點擊它時在其中一個類之間切換。如何切換指定了多個類的元素上的特定類?

<span class="icon-expand desktop-only mt8 pl4" id="toggle-site-content-width"></span> 

當我點擊這個,我只是需要改變icon-expandicon-reduce,和剛剛離開的其他類原樣。

它只是在我使用$(this).toggleClass('icon-reduce')時最後添加了該類。當我這樣做時也是如此:$(this).toggleClass('icon-expand','icon-reduce')

這是全碼:

$(function(){ 
    $('#toggle-site-content-width').click(function(){ // on click 
     $('#site-content').children('div').toggleClass('content-align-center'); // remove this class to expand the view to full available window width 
     $(this).toggleClass('toggle-expand','icon-reduce'); // change the icon 
     $(this).trigger('resize'); // trigger the window to rearrange masonry bricks 
    }); 
}); 

回答

2

你需要改變:

$(this).toggleClass('toggle-expand','icon-reduce'); 

到:

$(this).toggleClass('toggle-expand icon-reduce'); 

你不需要通過,這裏你的類分開。

+0

第一個人會做什麼。或者是一個語法錯誤。? –

+0

是的,這是在兩個類之間切換的錯誤方法。 – Felix

+0

哦。好的。非常感謝。 – ThomasK

相關問題