[英]How can i save the dark mode value in browsers ( using jquery )
the idea is I've a dark mode,i done with it ,but the problem ,when i reload the page the dark mode gone 这个想法是我有一个暗模式,我已经完成了,但是问题是,当我重新加载页面时,暗模式就消失了
this is HTML code 这是HTML代码
<input type="checkbox" id="darkMode" name="">
and this is js code ( with this way doesn't run ) 这是js代码(这种方式无法运行)
var ele = 'body';
$('#darkMode').on('click',function(){
if( $('#darkMode').prop('checked') ){
$(ele).addClass('dark-mode');
// here i wanna save the value of darkMode in browser
$.cookie('darkMode', 'dark', { expires: 7, path: '/' });
}else{
$(ele).removeClass('dark-mode');
}
});
thanks ! 谢谢 !
You should add something like: 您应该添加类似以下内容:
$(document).ready(function() {
if($.cookie('darkMode')){
$(ele).addClass('dark-mode');
$('#darkMode').prop('checked',true)
}
});
It will check if the cookie exist and if it does then set the darkmode. 它将检查cookie是否存在,如果存在,则将其设置为暗模式。
Edit 编辑
if you want to remove the cookie
when you uncheck the checkbox, then use 如果您想在取消选中复选框时删除cookie
,则使用
$.removeCookie('darkMode', {
path: '/'
});
You can read cookie
of the document, check if darkMode
property is present using regular expression and add dark-mode
class to the body
您可以读取文档的cookie
,使用正则表达式检查darkMode
属性是否存在,并将dark-mode
类添加到body
if(document.cookie.match(/^(.*;)?\s*darkMode\s*=\s*[^;]+(.*)?$/)) {
$('body').addClass('dark-mode');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.