【问题标题】:adding cookie for jquery style switch为 jquery 样式开关添加 cookie
【发布时间】:2012-07-08 22:23:00
【问题描述】:

我有一个想要添加功能的按钮。当您单击按钮时,网站的样式将变为高对比度版本(即样式表 high_contrast.css 被附加到头部)。显然我做错了一些事情,因为下面的代码只是切换当前页面的样式,当您导航到另一个页面时,它会切换回默认样式。我可能不应该每次都设置那个变量 highContrast。我想使用查询 cookie 插件 (https://github.com/carhartl/jquery-cookie) 来完成此操作,但在这种情况下不太了解如何使用它。

这是 HTML

<div id="contrast-btn"><a href="#" rel="css/high-contrast.css">high contrast</a></div>

这是脚本

$(document).ready(function(){
    var highContrast = false;
    $("#contrast-btn a").click(function () {
        if (!(highContrast)) {
            $('head').append('<link rel="stylesheet" href="css/high-contrast.css" type="text/css" id="hc_stylesheet"/>');
            highContrast = true;
        }       
        else {
            // remove the high-contrast style
            $("#hc_stylesheet").remove();
            highContrast = false;
        }
    });
});

感谢您的帮助

【问题讨论】:

    标签: jquery css cookies styles switch-statement


    【解决方案1】:

    您可以在全局上下文中设置highContrast,这将有助于您稍后在同一页面上评估

    var highContrast = false;
    $(document).ready(function(){
        // [...]
        highContrast = true;
        // [...]
    });
    

    但该值会在每次页面刷新时丢失,因此您可以 - 如您所愿 - 使用 jquery-cookie 设置 cookie

    $.cookie('highContrast', 'true', { path: '/' });
    

    并在页面加载时阅读:

    if($.cookie('highContrast') && $.cookie('highContrast') === "true") {};
    

    通过设置path = '/',cookie 将在整个域中可用。

    所以你的代码会变成这样:

    $(document).ready(function(){
        // Append the stylesheet on page load
        if ($.cookie('highContrast') === "true") {
            $('head').append('<link rel="stylesheet" href="css/high-contrast.css" type="text/css" id="hc_stylesheet"/>');
        }
        // Add the click handler to switch the stylesheet on and off
        $("#contrast-btn a").click(function () {
            if (!($.cookie('highContrast') === "true")) {
                $('head').append('<link rel="stylesheet" href="css/high-contrast.css" type="text/css" id="hc_stylesheet"/>');
                $.cookie('highContrast','true',{path:'/'});
            }       
            else {
                // remove the high-contrast style
                $("#hc_stylesheet").remove();
                $.cookie('highContrast','false',{path:'/'});
            }
        });
    });
    

    【讨论】:

    • 这在 WordPress 环境中对我来说是开箱即用的,而另一个则没有。非常感谢。
    【解决方案2】:

    您必须通过 cookie 获取和设置值:

    $(document).ready(function(){
    // DRY wrapper function
    function appendStyleSheet() {
      $('head').append('<link rel="stylesheet" href="css/high-contrast.css" type="text/css" id="hc_stylesheet"/>'); 
    }
    // append the style sheet on load if the cookie is set to true
    if ($.cookie('high_contrast') == 'true') {
      appendStyleSheet();      
    }
    $("#contrast-btn a").click(function () {
        if ($.cookie('high_contrast') != 'true') {
    
            appendStyleSheet();      
            $.cookie('high_contrast', 'true'); // set the cookie to true
        }       
        else {
            // remove the high-contrast style
            $("#hc_stylesheet").remove();
            $.cookie('high_contrast', 'false');
        }
    });
    });
    

    您可以为 cookie 添加过期或站点范围内的有效性等选项,因此如果您希望 cookie 有效期为一年,请将其添加到 cookie 命令中

    $.cookie('high_contrast', 'false', {expires: 365});
    

    如果您希望它在您的整个域中有效(这很可能是您的实施的情况),您可以添加路径“/”:

    $.cookie('high_contrast', 'false', {path: '/'});
    

    【讨论】:

    • 似乎在这一行抛出错误: if (($.cookie('high_contrast') == 'true') {
    • 那里对我来说工作正常,谢谢。一个问题,当您实际设置 cookie 时,我是否应该将 cookie 设置为过期,即 $.cookie('high_contrast','true', {expires:365}); ?
    • 取决于您希望它何时过期。 expires: 后的数字以天为单位,因此在设置 cookie 的位置(使用两个参数调用 $.cookie -> $.cookie('high_contrast', 'true'),您应该将 {expires: daysYouWantItToExpireIn} 作为第三个参数。过期的默认值是会话,这意味着只要用户停留在页面上。
    • 如果不是很明显,如果有人在线上遇到错误:if ($.cookie('high_contrast') == 'true') { 错误为:“Uncaught TypeError: undefined不是一个功能,您可能还没有包含正确的 jquery-cookie 插件。
    猜你喜欢
    • 1970-01-01
    • 2011-10-09
    • 2014-11-19
    • 2012-04-07
    • 2019-04-11
    • 2011-09-24
    • 2012-03-19
    • 2011-12-02
    • 1970-01-01
    相关资源
    最近更新 更多