【问题标题】:Store jquery .click as a cookie将 jquery .click 存储为 cookie
【发布时间】:2014-06-04 00:32:11
【问题描述】:

下午,提前感谢那些喜欢尝试一下的人。

我有一个客户稍微改变了他们的要求,现在希望在他们的网站上添加一些东西。

我使用 jquery .click 更改网站上的大量 css,以便为有视力问题的人提供不同级别的可访问性。

这是网站,换色器在顶部。 http://www.dsndev.dewinterdev.co.uk

<div id="colour-scheme">
<p>
    Colour Scheme <span class="default"><button onclick="reloadPage()">A</button></span>
    <span class="white"><button>A</button></span>
    <span class="yellow"><button>A</button></span>
</p>

这是代码`http://jsfiddle.net/ZNuWg/

我想将信息存储在一个 cookie 中,这样当用户单击按钮时,他们可以返回到具有正确配色方案的站点。

【问题讨论】:

  • 抱歉,这是一种非常可怕的硬连线方式来更改主题。只需更改 DOM 顶部的单个类(正文?HTML?等)并通过 CSS 完成所有操作。如果您将现有的 CSS 文件移动到使用 LESS,那么管理甚至非常深的 CSS 层次结构变得非常容易。
  • 同样的老故事,如果事情从一开始就更清楚一点,它可能会有所不同。这也是我第一次使用 jquery,我慢慢地捡东西。感谢您的建议。
  • +1: 现在你有 15 个代表了。欢迎来到 SO :)

标签: jquery wordpress cookies themes


【解决方案1】:

虽然解决您关于点击和 cookie 的情况很简单,但请注意,我已经抽象出代码以对单独的函数进行所有更改以允许重用,我可能会将所有 css 更改移动到 2 个单独的 css文件并加载它们,而不是执行所有的 javascript。我已经从这里添加了 jQuery cookie 插件https://github.com/carhartl/jquery-cookie

小提琴:

http://jsfiddle.net/robschmuecker/ZNuWg/2/

Javascript:

jQuery(document).ready(function () {
    jQuery(".white button").click(function(){setWhite()});
    jQuery(".yellow button").click(function(setYellow()});

    //Do the cookie check 
    //Check color cookie value
    if (jQuery.cookie('color') == 'White') {
        setWhite();
    } else if (jQuery.cookie('color') == 'Yellow') {
        setYellow();
    }


    function setWhite() {
        //First set the cookie
        jQuery.cookie('color', 'White');

        //jQuery(".top-bar").css("border-bottom","0px solid #189AC2");
        //social links
        jQuery(".menu-logo img").attr('src', '/wp-content/themes/dsn/images/logo-white.png');
        jQuery(".logo img").attr('src', '/wp-content/themes/dsn/images/logo-white.png');
        jQuery(".facebook").css("background-image", "url(/wp-content/themes/dsn/images/social-widget-white.png)");
        jQuery(".twitter").css("background-image", "url(/wp-content/themes/dsn/images/social-widget-white.png)");
        jQuery(".pintrest").css("background-image", "url(/wp-content/themes/dsn/images/social-widget-white.png)");
        jQuery(".youtube").css("background-image", "url(/wp-content/themes/dsn/images/social-widget-white.png)");
        jQuery(".linkedin").css("background-image", "url(/wp-content/themes/dsn/images/social-widget-white.png)");
        jQuery(".google").css("background-image", "url(/wp-content/themes/dsn/images/social-widget-white.png)");
        jQuery("#bslvideoshow").css("background", "url(/wp-content/themes/dsn/images/bsl-video.png) repeat scroll 0 center rgba(0, 0, 0, 0)");
        //latest news section
        jQuery("#latest-news h2").css("color", "#189AC2");
        jQuery("#latest-news h2").css("border-bottom", "1px solid #189AC2");
        jQuery("#latest-news h3").css("color", "#189AC2");
        jQuery("#latest-news p").css("color", "#000000");
        jQuery("#latest-news a").css("color", "#189AC2");
        //twitter section
        jQuery("#index-twitter").css("background-image", "url(wp-content/themes/dsn/images/twitter-white.png)");
        jQuery("#index-twitter").css("background-size", "85% auto");
        jQuery("#index-twitter p").css("color", "#ffffff");
        jQuery("#index-twitter iframe").css("margin-left", "54px");
        //footer & copyright
        jQuery(".top-bar-full p").css("color", "");
        jQuery("#controls a").css("color", "");
        jQuery(".copyright ul li").css("border-right", "");
        //footer-section
        jQuery(".footer").css("border-top", "");
        jQuery("#bottom-grid h2").css("color", "");
        jQuery("#bottom-grid h3").css("color", "");
        jQuery("#latest-news h2").css("border-bottom");
        jQuery("#bottom-grid").css("color", "");
        jQuery("#bottom-grid p").css("color", "");
        jQuery("#bottom-grid a").css("color", "");
        //forms
        jQuery("#search button").css("background", "");
        jQuery("#keep-in-touch button").css("background", "");
        jQuery(".stayconnectedright i").css("background", "");
        //main-menu
        jQuery(".index-menu li").css("border", "none");
        jQuery(".index-menu li").css("padding", "57px 0");
        jQuery(".index-menu a").css("background", "");
        jQuery(".index-menu li a").css("border", "");
        jQuery(".index-menu .sub-menu a").css("border", "");
        jQuery(".index-menu .sub-menu").css("background", "");
        jQuery(".index-menu a").css("color", "");
        jQuery(".index-menu .sub-menu").css("border", "");
        jQuery(".the-content p").css("color", "#000000");
        jQuery(".the-content h2").css("color", "#000000");
        jQuery(".sub-menu-wrap").css("background", "#ffffff");
        jQuery(".sub-menu-wrap span").css("color", "#000");
        jQuery(".download-module a").css("color", "#000");
        jQuery(".section-title").css("color", "#000");
        jQuery(".section-title").css("border-top", "1px solid #000");
        jQuery(".main-menu li").css("background", "");
        jQuery(".menu-item-389").css("border", "");
        jQuery(".content-navigation ul li").css("border", "");
        jQuery("#latest-news h2").css("color", "#000");
        jQuery(".title").css("color", "#000");
        jQuery("#latest-news h2").css("border-bottom", "1px solid#000");
        jQuery("#latest-news a").css("color", "#000");
        jQuery(".the-content a").css("color", "#000");
        jQuery(".the-content a").css("text-decoration", "underline");
        jQuery(".sd-title").css("color", "#000 !important");
        jQuery(".the-date").css("color", "#fff");
        jQuery(".archive-nav a").css("color", "#000");
        jQuery(".archive-nav a:hover").css("font-weight", "bold");
        jQuery(".archive-nav").css("border-bottom", "1px solid #000");
        jQuery(".footer").css("border-top", "none");
        jQuery(".content-navigation li a").css("color", "#fff");
        //top and bottom background colours
        var bodyclass = $('body').attr('class');
        jQuery(".top-bg-white").addClass(bodyclass);
        jQuery(".bottom-bg-white").addClass(bodyclass);
        jQuery("body").css("background", "none repeat scroll 0 0 #FFFFFF");
        jQuery(".madeby").css("color", "#000");
        jQuery(".post-board p").css("color", "#000");
        jQuery(".post-board a").css("color", "#000");
        jQuery(".post-board a").css("text-decoration", "underline");
        jQuery(".the-date p").css("color", "#fff");
        jQuery(".madeby").css("color", "#fff");
        jQuery(".archive-nav ul li").css("background", "url(/wp-content/themes/dsn/images/bullet-keyline1.png) no-repeat scroll 0 0 rgba(0, 0, 0, 0)");
        jQuery(".archive-nav .current-menu-item").css("background", "url(/wp-content/themes/dsn/images/bullet-keyline.png) no-repeat scroll 0 0 rgba(0, 0, 0, 0)");
        jQuery(".top-bg-yellow").css("display", "");
        jQuery(".bottom-bg-yellow").css("display", "");
        jQuery(".footer-logos img").attr("src", "");
    }

    function setYellow() {

        //First set the cookie
        jQuery.cookie('color', 'Yellow');

        jQuery("body").css("background", "none repeat scroll 0 0 #f7e463");
        jQuery(".top-bg-yellow").css("display", "block");
        jQuery(".bottom-bg-yellow").css("display", "block");
        jQuery(".menu-logo img").attr("src", "/wp-content/themes/dsn/images/logo-yellow.png");
        //social links
        jQuery(".logo img").attr('src', '/wp-content/themes/dsn/images/logo-yellow.png');
        jQuery(".facebook").css("background-image", "url(/wp-content/themes/dsn/images/social-widget-yellow.png)");
        jQuery(".twitter").css("background-image", "url(/wp-content/themes/dsn/images/social-widget-yellow.png)");
        jQuery(".pintrest").css("background-image", "url(/wp-content/themes/dsn/images/social-widget-yellow.png)");
        jQuery(".youtube").css("background-image", "url(/wp-content/themes/dsn/images/social-widget-yellow.png)");
        jQuery(".linkedin").css("background-image", "url(/wp-content/themes/dsn/images/social-widget-yellow.png)");
        jQuery(".google").css("background-image", "url(/wp-content/themes/dsn/images/social-widget-yellow.png)");
        jQuery("#bslvideoshow").css("background", "url(/wp-content/themes/dsn/images/bsl-video-yellow.png) no-repeat scroll 0 center rgba(0, 0, 0, 0)");
        //latest news section
        jQuery("#latest-news h2").css("color", "#000000");
        jQuery("#latest-news h2").css("border-bottom", "1px solid #000000");
        jQuery("#latest-news h3").css("color", "#000000");
        jQuery("#latest-news p").css("color", "#000000");
        jQuery("#latest-news a").css("color", "#000000");
        //twitter section
        jQuery("#index-twitter").css("background-image", "url(wp-content/themes/dsn/images/twitter-yellow.png)");
        //footer-section
        jQuery(".footer").css("border-top", "1px solid #000000");
        jQuery("#bottom-grid h2").css("color", "#000000");
        jQuery("#bottom-grid h3").css("color", "#000000");
        jQuery("#latest-news h2").css("border-bottom", "1px solid #000000");
        jQuery("#bottom-grid").css("color", "#000000");
        jQuery("#bottom-grid p").css("color", "#000000");
        jQuery("#bottom-grid a").css("color", "#000000");
        //top bar
        jQuery(".top-bar a").css("color", "#000000");
        jQuery(".top-bar p").css("color", "#000000");
        jQuery(".top-bar").css("border-bottom", "1px solid #000000");
        //forms
        jQuery("#search button").css("background", "#000000");
        jQuery("#keep-in-touch button").css("background", "#000000");
        jQuery(".stayconnectedright i").css("background", "#000000");
        //main-menu
        jQuery(".index-menu li ").css("background", "#F7E463");
        jQuery(".index-menu li ").css("border", "10px solid #000000");
        jQuery(".index-menu li").css("padding", "");
        jQuery(".index-menu .sub-menu").css("border", "0px");
        jQuery(".index-menu .sub-menu li").css("border", "0px");
        jQuery(".index-menu .sub-menu-wrap").css("background", "#F7E463");
        jQuery(".index-menu a").css("color", "#000");
        //content navigation
        jQuery(".main-menu li").css("background", "#F7E463");
        jQuery(".content-navigation ul li").css("border", "3px solid #000");
        jQuery(".sub-menu-wrap ul li").css("border", "none");
        jQuery(".menu-item-389").css("border", "none");
        jQuery("ul li a").css("color", "#000000");
        jQuery(".the-content p").css("color", "#000000");
        jQuery(".the-content h2").css("color", "#000000");
        jQuery(".sub-menu-wrap").css("background", "#F7E463");
        jQuery(".sub-menu-wrap span").css("color", "#000");
        jQuery(".download-module a").css("color", "#000");
        jQuery(".section-title").css("color", "#000");
        jQuery(".section-title").css("border-top", "1px solid #000");
        jQuery("h3").css("color", "#000000");
        //copyright
        jQuery(".copyright ul li").css("border-right", "1px solid #000000");
        jQuery(".footer-logos img").attr("src", "/wp-content/themes/dsn/images/logos-yellow.png");
        jQuery(".wpcf7-form-control.wpcf7-submit").css("background", "none repeat scroll 0 0 #000");
        jQuery(".the-content a").css("color", "#000");
        jQuery(".the-content a").css("text-decoration", "underline");
        jQuery(".title").css("color", "#000");
        jQuery(".default button").css("border", "1px solid #000");
        jQuery(".madeby").css("color", "#000");
    }
});

【讨论】:

  • 感谢您花时间在这里帮助我。我是您建议的插件的新手,除了将脚本添加到我的网站然后使用上面的代码之外,我还需要做很多事情吗?或者我需要从 github 上的说明中考虑其他选项和代码吗?
  • 注意:显示的点击处理程序已损坏。他们不应该调用函数,他们应该只是传递函数。
  • @TrueBlueAussie 当然!傻我!将更新代码
  • @AlexKnopp 不,您可以只在 github 上包含 jquery.cookie.js 上的脚本,然后在您的页面上包含 jquery,然后上面的代码应该可以解决问题。 github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js
  • 好吧,这很好用。网站上最常见的页面将以用户需要的正确方式显示。但是,请问有没有办法将这种 cookie 设置为应用于网站上的每个页面?
【解决方案2】:

我建议您如何处理动态主题,而不是硬编码更改:

JSFiddle:http://jsfiddle.net/NLn4a/1/

HTML:

<div id="colour-scheme">
    <p>Colour Scheme 
    <span class="default"><button>default</button></span>
    <span class="white"><button>White</button></span>
    <span class="yellow"><button>Yellow</button></span>
    </p>
</div>

jQuery 只是改变了 body 元素上的一个类

$(function () {
    $('#colour-scheme').on('click', 'button', function () {
        $('body').removeClass('default white yellow').addClass($(this).parent().attr('class'));
    });
});

此代码只是从按钮的父级获取class 并将其添加到body 元素中。

CSS(特定于每个层次结构):

body.default {
    background-color: black;
    color: white;
}
body.white {
    background-color: white;
    color: blue;
}
body.yellow {
    background-color: yellow;
    color: green;
}

这会将样式维护移到它所属的位置(到样式中)。

Cookie:

您可以在加载时将 cookie 值作为类名恢复到 body 元素中:

例如$('body').addClass(cookieValue);

合起来就是:

$(function () {
    var cookieValue = getMyCookieValueCodeHere;
    $('body').addClass(cookieValue);
    $('#colour-scheme').on('click', 'button', function () {
        $('body').removeClass('default white yellow').addClass($(this).parent().attr('class'));
    });
});

早熟:

您可能希望在页面开始加载后立即在页面上设置主题 class。您可以在页面 head 中使用原始 javascript 来执行此操作(当时 jQuery 尚未准备好)。

<script type="text/javascript>
    // read cookie value with raw javascript
    document.body.className += cookievalue;
</script>

可选的“默认”主题:

您的“默认”主题不需要使用类(例如默认),而只是没有任何 body.class 选择器的任何基本 CSS。即您当前的原始 CSS。

【讨论】:

  • 你可以把马牵到水边……把它的头压在下面10分钟……但它仍然……不会……喝水!: )
  • 感谢您的详细说明。我会保存它并再次使用它。但我真的没有时间回到这个绘图板上。有没有办法获取和设置 cookie 并全局加载它,以便用户可以在主页上设置它并在整个站点中使用它。我正在使用 Rob Schmuecker 的回答。谢谢。
  • 关于“我真的没有时间”:从长远来看,你会花更多的时间在代码中做这件事(这样会有更多的错误)。重新饼干:当然,这就是饼干的用途。默认情况下,它们对站点是全局的。如果您想了解具体情况,您需要提出一个新问题。 PS。 向上箭头按钮是为了感谢 SO :)
  • 如果 cookie 在默认情况下对站点来说是全局的,那么它是如何一次只能工作一页的呢?我知道我应该从一开始就正确地做到这一点,但现在时间真的对我不利。我的大部分学习都是我犯了一些错误,我知道我需要在未来正确地做到这一点。
  • 正如我所说的关于 cookie “如果你想了解细节,你需要问一个新问题”。 PS。向上箭头按钮仍然是为了感谢 SO(你现在有 15 个代表,所以可以使用它们):)
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
  • 2016-04-10
  • 2012-07-14
  • 2018-10-06
相关资源
最近更新 更多