我正在研究宽度更改器(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/