jQuery cookie:如何保存用户下次访问的颜色选择? [英] jQuery cookie: How can I save the color selection of the user for the next visit?

查看:126
本文介绍了jQuery cookie:如何保存用户下次访问的颜色选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究jQuery预定义的颜色选择器.当用户单击我的颜色选择器的一种颜色时,我想保存颜色选择: http://prntscr. com/7rnafa .为了与Cookie进行交互,我在 https://github.com/carhartl/jquery-cookie上使用jQuery插件.

I'm working on a jQuery predefined color picker. I'd like to save the color selection when the user click on one of the colors of my color picker: http://prntscr.com/7rnafa . To interact with cookies, I am using the jQuery plugin at https://github.com/carhartl/jquery-cookie .

jQuery代码:

var color_elements_background = ".nodeList .categoryStrip, .breadcrumb";

var color_elements_text = "a:link, a:visited";

$(".colorPicker span").on("click", function()
{
    var customColor = $(this).attr("custom_color");
    $(color_elements_background).css("background-color", customColor);
    $(color_elements_text).css("color", customColor);
});
});

我的颜色选择器的HTML:

HTML of my color picker:

                <div class="colorPicker">
                    <div class="colorPickerContent">
                        <div class="colorPickerItems">
                            <li>
                                <span custom_color="#FF0000">Color 1</span>
                            </li>
                            <li>
                                <span custom_color="#333333">Color 2</span>
                            </li>
                            <li>
                                <span custom_color="#FFFFFF">Color 3</span>
                            </li>
                            <li>
                                <span custom_color="#D0D0D0">Color 4</span>
                            </li>
                            <li>
                                <span custom_color="#CCCCCC">Color 5</span>
                            </li>
                        </div>
                    </div>
                </div>

推荐答案

查看插件:

https://github.com/js-cookie/js-cookie

要使用Javascript库js.cookie保存cookie,请使用:

To save a cookie using the Javascript library js.cookie you would use:

Cookies.set("color", customColor);

从保存的cookie中获取值的方法还可以是:

Also to get the value from the saved cookies would be:

var customColor = Cookies.get("color");

如果您想删除该Cookie,则可以使用:

If you ever wanted to remove that cookie you would use:

Cookies.remove("color");

这篇关于jQuery cookie:如何保存用户下次访问的颜色选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆