javascript cookie 保存和加载
我有这段 javascript 代码可以改变网站的风格。但此代码仅在您单击它时更改样式。刷新页面后,它会恢复默认状态。我希望它保存用户的偏好。我真的很喜欢这段代码,我不想用另一段代码来改变它。你能帮忙吗?我希望你在添加cookies代码后给我代码!
$(document).ready(function() {
$("#fullswitch").click(function() {
$("#chpheader").removeClass("compact");
$("#imgg").removeClass("compact");
$("#chpheader").removeClass("original");
$("#imgg").removeClass("original");
$("#chpheader").addClass("normal");
$("#imgg").addClass("normal");
});
$("#compactswitch").click(function() {
$("#chpheader").addClass("compact");
$("#imgg").addClass("compact");
$("#chpheader").removeClass("original");
$("#imgg").removeClass("original");
$("#chpheader").removeClass("normal");
$("#imgg").removeClass("normal");
});
$("#originalswitch").click(function() {
$("#chpheader").addClass("original");
$("#imgg").addClass("original");
$("#chpheader").removeClass("compact");
$("#imgg").removeClass("compact");
$("#chpheader").removeClass("normal");
$("#imgg").removeClass("normal");
});
});
I have this javascript code that changes the style of the website. but this code changes the style only when you click on it. and after you refresh the page it returns to default. I want it to save the users preferences. I really like the code and I do not want to change it with another one. can you please help. I want you to give me the code after adding the cookies code to it!
$(document).ready(function() {
$("#fullswitch").click(function() {
$("#chpheader").removeClass("compact");
$("#imgg").removeClass("compact");
$("#chpheader").removeClass("original");
$("#imgg").removeClass("original");
$("#chpheader").addClass("normal");
$("#imgg").addClass("normal");
});
$("#compactswitch").click(function() {
$("#chpheader").addClass("compact");
$("#imgg").addClass("compact");
$("#chpheader").removeClass("original");
$("#imgg").removeClass("original");
$("#chpheader").removeClass("normal");
$("#imgg").removeClass("normal");
});
$("#originalswitch").click(function() {
$("#chpheader").addClass("original");
$("#imgg").addClass("original");
$("#chpheader").removeClass("compact");
$("#imgg").removeClass("compact");
$("#chpheader").removeClass("normal");
$("#imgg").removeClass("normal");
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你做错了。您应该将一个类添加到顶级元素(例如根元素 (
)),并根据该元素对文档的其余部分进行样式设置。
应该变成
这样,你只需要改变一个类,整个文档就会改变。这也意味着您只需要保存一个 cookie(描述一个类),并且只加载一次。
jsFiddle 示例
编辑:jsFiddle 示例更新为包含 Cookie。
EDIT2:更新了 jsFiddle 示例以包含多种样式!
You're doing it wrong. You should add a class to a top-level element such as the root (
<html>
) and style the rest of your document according to that.Should become
That way, you only need to change one class, and the whole document will change. It will also mean you only need to save one cookie (that describes that one class), and load it only once.
jsFiddle Example
EDIT: jsFiddle example updated to include Cookies.
EDIT2: jsFiddle example updated to include multiple stylings!
cookie 只是一个键值对,因此您可以为每个正在设计样式的元素拥有一个 cookie,其中键是 id,值是类名。在 jQuery 中,你可以像这样设置它们:
然后在你的文档顶部准备好你会:
但是警告的话,你的代码已经有点混乱了,如果你进去并开始设置和取消设置 cookie 到处都是会变得非常马虎。我建议尝试提取一些功能,并设置观察者模式。另外,你真的需要正常上课吗?如果你这样做,当你添加紧凑类时你真的需要摆脱它吗?
A cookie is just a key value pair so you can have one cookie per element you are styling where the key is the id and the value is the class name. In jQuery you would set them like this:
and then at the top of your doc ready you would have:
but word of warning, your code is already a little messy, if you go in and start setting and unsetting cookies all over the place things are going to get really sloppy. I would recommend trying to pull out some functions, and set up the observer pattern. Also, do you really need a normal class? and if you do do you really need to get rid of it when you add the compact class?