如何将自定义宽度保存到Cookie? [英] How can I save my custom width to a cookie?
问题描述
我正在研究宽度转换器.我为此使用jQuery,但我遇到了问题.这个想法是,当用户单击按钮时,宽度立即改变.我想将用户的选择保存在cookie中,但是我不知道该怎么办.
I'm working on a width changer. I use jQuery for this, but I've a problem. The idea is that when the user clicks the button, the width changes instantly. I'd like to save the user's selection in a cookie, but I don't know how I can do.
当前的jQuery代码:
Current jQuery code:
$(function()
{
$(".width-changer").on("click", function(e)
{
$(".wrapper").toggleClass("custom-width");
});
});
我将此插件用于cookie:
I use this plugin for the cookie:
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.3.1/jquery.cookie.js"></script>
它在我的index.html中.
It is in my index.html.
包装器类是我的全局宽度.
The wrapper class is my global width.
推荐答案
这是在i.explorer&中使用的解决方案. Firefox,对于Chrome浏览器,我认为您应该启用Cookie:
this is the solution worked for me in i.explorer & Firefox, for chrome i think you should to enable cookies :
$(function()
{
if ($.cookie('custom_width')=='true') { //add true to this condition
$(".wrapper").addClass("custom-width");
};
$(".width-changer").on("click", function(e)
{
$(".wrapper").toggleClass("custom-width");
if($(".wrapper").hasClass("custom-width"))
$.cookie('custom_width', 'true');
else
$.cookie('custom_width', 'false');
});
});
希望此解决方案对您有所帮助.
Hope that this solution help you.
这篇关于如何将自定义宽度保存到Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!