简体   繁体   English

jQuery cookie:如何保存用户下次访问的颜色选择?

[英]jQuery cookie: How can I save the color selection of the user for the next visit?

I'm working on a jQuery predefined color picker. 我正在研究jQuery预定义的颜色选择器。 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 . 当用户单击我的颜色选择器的一种颜色时,我想保存颜色选择: http : //prntscr.com/7rnafa To interact with cookies, I am using the jQuery plugin at https://github.com/carhartl/jquery-cookie . 为了与Cookie进行交互,我在https://github.com/carhartl/jquery-cookie上使用jQuery插件。

jQuery code: 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 of my color picker: 我的颜色选择器的HTML:

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

See the plugin: 参见插件:

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

To save a cookie using the Javascript library js.cookie you would use: 要使用Javascript库js.cookie保存cookie,请使用:

Cookies.set("color", customColor);

Also to get the value from the saved cookies would be: 同样从保存的cookie中获取值的方法是:

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

If you ever wanted to remove that cookie you would use: 如果您想删除该Cookie,则可以使用:

Cookies.remove("color");

Use the cookie library to store the previous selection, and populate the DOM using $( document ).ready(function(){}. 使用Cookie库存储先前的选择,并使用$(document).ready(function(){})填充DOM。

If your cookie is not saving, confirm the domain which the cookie is set. 如果您的cookie没有保存,请确认已设置cookie的域。 Often the cookie domain will not match your site when setting a cookie. 设置cookie时,cookie域通常与您的网站不匹配。 Note that cookies will not save if you are using http://localhost . 请注意,如果您使用http:// localhost ,则cookie不会保存。 Use "Resources" tab within Chrome Dev tools to confirm your cookie is set. 使用Chrome开发人员工具中的“资源”标签来确认您的Cookie已设置。

ps If you want someone to do your work for you, consider freelancer.com or odesk. ps如果您希望某人为您完成工作,请考虑使用freelancer.com或odesk。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM