主题切换器无法正常工作,没有cookie

时间:2015-07-03 14:24:37

标签: jquery

我正在使用此处显示的此方法http://leihu.com/thoughts/e/switching-styles-silently

这是我的头脑

<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。

1 个答案:

答案 0 :(得分: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;
    });

});