2015-07-03 58 views
-1

我使用這裏顯示http://leihu.com/thoughts/e/switching-styles-silently主題切換器不工作,沒有Cookie

下面這個方法是我的頭

<link href="default.css" title="common-styles" type="text/css" rel="stylesheet" class="theme"> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" type="text/javascript"></script> 
<script src="jquery.cookie.js" type="text/javascript"></script> 

這裏是我的HTML和腳本樣式表切換。當它在兩張紙之間切換回來和第四次時,在頁面刷新時不記得最後選擇的樣式表。

<ul class="themes"> 
    <li><a href="#" rel="default.css">Default</a></li> 
    <li><a href="#" rel="alternative.css">Alternative</a></li> 
</ul> 


<script type="text/javascript"> 
$(document).ready(function(){ 
     $('.themes a').on('click',function(){ 
      $('.theme').attr('href',$(this).attr('rel')); 
      $.cookie('theme-cookie',$(this).attr('rel'), {expires: 365, path: '/', domain: '.mysite.com'}); 
      return false; 
     }); 

}); 
</script> 

我已經改變了mysite.com到我自己的網站OFC,我想我已經正常,但沒有做的一切設置出於某種原因的cookie。

回答

2

你忘了把它當加載頁面:

$(document).ready(function(){ 
    // ============== 
    // theme switcher 
    // ============== 

    // load css from cookie! 
    $(".theme").attr("href", $.cookie('theme-cookie')); 

    // listen for clicks on the a links within .themes 
    $('.themes a').on('click',function(){ 
     // change the href of link.theme to match the rel of this 
     $('.theme').attr('href',$(this).attr('rel')); 
     // set a cookie to remember 
     $.cookie('theme-cookie', $(this).attr('rel'), {expires: 365, path: '/', domain: '.nitrografixx.com'}); 
     // prevent this from reloading the browser 
     return false; 
    }); 

}); 
+0

檢查我的更新答案! @MShack ... –

+0

好吧,我試過了,並沒有工作。刪除所有可選選項。 – MShack

+0

@MShack由於Spry,控制檯中存在很多錯誤。可能會導致你的其他JS功能失敗! –