2012-07-02 41 views
2

我有以下幾點:如何檢測類的存在並添加或刪除其他類?

<html lang="en" class="no-js theme-light"> or 
<html lang="en" class="no-js xxx yyy theme-light"> 

我願讓這個點擊一個鏈接將其更改爲:

<html lang="en" class="no-js theme-dark"> 
<html lang="en" class="no-js xxx yyy theme-dark"> 

再次點擊將其改回。即刪除theme-dark並添加theme-light

有人可以解釋我怎麼能用jQuery做到這一點?

$('#sidebar-theme-switch').click(function (e) { 
    ??? 
}); 

回答

5
$('#sidebar-theme-switch').click(function() { 
    $('html').toggleClass('theme-light theme-dark') 
}); 

LIVE DEMO

toggleClass

描述:添加或,刪除從每個元素的一個或多個類別中匹配的元素取決於任一類的存在或值的開關參數。

相關問題