javascript - 如何将自定义宽度保存到 cookie 中?

标签 javascript jquery cookies

我正在研究宽度更改器(mutator)。我使用 jQuery 来实现此目的,但我有一个问题。这个想法是,当用户单击按钮时,宽度立即改变。我想将用户的选择保存在 cookie 中,但我不知道该怎么做。

当前 jQuery 代码:

$(function()
{
  $(".width-changer").on("click", function(e)
  {
    $(".wrapper").toggleClass("custom-width");
  });
});

我使用这个插件来处理 cookie:

<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.3.1/jquery.cookie.js"></script>

它在我的index.html 中。

包装类是我的全局宽度。

最佳答案

这是在 i.explorer 和 Firefox 中为我工作的解决方案,对于 chrome,我认为你应该启用 cookie:

$(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');

  });
});

希望这个解决方案对您有所帮助。

关于javascript - 如何将自定义宽度保存到 cookie 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31305393/

相关文章:

laravel - Laravel 上的多个 SESSION_DOMAIN

javascript - 为什么语句 if ( !condition ) { console.log(condition) } 显示 true

javascript - 通过消息激活 Chrome 扩展程序后台页面

javascript - 仅清除表单中的 &lt;textarea&gt;

javascript - Highcharts加载点从JSON开始。毫秒转换为 UTC 时间?

javascript - jQuery - 使用删除图像从数组中删除 li

javascript - ATL ActiveX 控件如何向 javascript 公开一个 bool 值

javascript - 文本未按正确顺序显示

javascript - 语法错误: Unexpected token < in JSON at position 8

javascript - 使用 javascript 获取 AspNet.ApplicationCookie