[英]Trouble saving current state of css class in local storage
I am using two buttons to toggle css classes that either show or hide a background. 我正在使用两个按钮来切换显示或隐藏背景的CSS类。 I would like to save the current state of each class in local storage and display (or not display) them when the user returns. 我想将每个类的当前状态保存在本地存储中,并在用户返回时显示(或不显示)它们。 I have made an attempt with the wrapping
class in this jsFiddle, but I don't have it working yet: 我在这个jsFiddle中尝试了wrapping
类,但是我还没有使用它:
http://jsfiddle.net/eqrk6fak/1/ http://jsfiddle.net/eqrk6fak/1/
Setting it like so: 设置如下:
$("#switch2").click(function() {
wrap.fadeToggle(600);
if($('div').hasClass(wrap)){
localStorage.setItem('background', wrap);
}
});
And then trying to get it when the user returns: 然后在用户返回时尝试获取它:
$(document).ready(function(){
var background = localStorage.getItem('background');
if(background !== ''){
$('#wraping').addClass(background);
}
});
Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!
The problem is in the following line: 问题在以下行中:
$('div').hasClass(wrap)
Reviewing your JSFiddle code, at that point wrap
is a jQuery result: 查看您的JSFiddle代码,这时的wrap
是jQuery的结果:
var wrap = $(".wraping");
According to the documentation , jquery.hasClass
receives a className
which should be a String
. 根据文档 , jquery.hasClass
接收一个className
,它应该是一个String
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.