关于cookies
和localStorage
(设置、删除...)的文章太多了,但我不清楚我应该把代码放在哪里。
即,我试图通过单击按钮来更改 css 样式表,但无法在下次加载页面时保存该选择。像这样的东西:
参数.php
if (user clicked on "btntheme") {
$theme = 'green.css';
}else{
$theme = "blue.css";
};
索引.php
<?php include ("params.php");?>
<head>
<link id="link01" rel="stylesheet" href="<?php echo $theme;?>">
</head>
<body>
<div id="btntheme">THEME</div>
</body>
JS
$('#btntheme').click(function(){
$('#link01').attr('href', 'green.css');
});
这会更改样式,但仅限于当前 session 。我如何告诉 params.php
将来 $theme
是 green.css
,而不是 blue.css
?
最佳答案
使用 localStorage 非常简单。
设置
localStorage.setItem('theme','green');
得到
localStorage.getItem('theme');
所以你可以在一个函数中使用它从 LS 获取然后设置你的样式表。
function changeTheme(){
var theme = localStorage.getItem('theme');
$('#link01').attr('href', theme + '.css');
}
$('#btntheme').click(function(){
localStorage.setItem('theme','green');
changeTheme();
});
您也可以在 document.ready
函数中调用该函数以在页面加载时设置它。
关于php - 如何记住样式变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41306666/