【问题标题】:How to add a cookie to save style selected by user Jquery如何添加 cookie 以保存用户 Jquery 选择的样式
【发布时间】:2011-10-09 22:34:04
【问题描述】:

我在我的网站上添加了一个样式切换器,我想添加一个 cookie 来保存用户最后选择的样式。我有这个代码,有人可以指导我吗?

感谢阅读!

我有这段代码可以在我的网站上切换样式:

HTML 调用主样式和主颜色

    <style type="text/css"> @import url("style.css");</style>
    <link href="yellow.css" rel="stylesheet" type="text/css" title="Yellow Theme" />

然后我照常调用脚本

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript" src="script.js"></script>

每个按钮都有这个:

    <a class="colorbox colorred" href="?theme=red" title="Red Theme"><img src="red.png" width="x" height="x" /></a>
    <a class="colorbox colorblack" href="?theme=black" title="Black Theme"><img src="black.png" width="x" height="x" /></a>

现在是 script.js 代码:

    google.load("jquery", "1.5.2");
    google.setOnLoadCallback(function()
    {

    // Color changer
    $(".colorblack").click(function(){
        $("link").attr("href", "black.css");
        return false;
    });

    $(".colorred").click(function(){
        $("link").attr("href", "red.css");
        return false;
    });



        });

【问题讨论】:

    标签: jquery cookies stylesheet setcookie css


    【解决方案1】:

    这样的?未经测试:

    $(function() {
    
        if($.cookie("style") !== null) {
            $('link').attr('href', $.cookie("style"));
        }
    
        $('a').each(function() {
            $(this).click(function(e) {
                e.preventDefault();
                var css = $(this).attr('href');
                css = css.split('=');
                $('link').attr('href', ''+ css[2] +'.css');
                $.cookie("style", ""+ css[2] +".css", { path: '/', expires: 365 });
            });
        });
    
    });
    

    它将需要jQuery Cookie Plugin

    【讨论】:

    • 嗨,谢谢,我测试了它,但没有用。但我已经找到了解决方案。它不仅仅是 jquery php,而是完美的工作
    猜你喜欢
    • 2016-09-06
    • 2013-04-22
    • 1970-01-01
    • 2012-07-08
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    相关资源
    最近更新 更多