有這麼多關於cookies
和localStorage
(設置,刪除...)的文章,但是我不清楚我應該在哪裏放置一些代碼。如何記住樣式更改?
即,我試圖改變CSS樣式表單擊一個按鈕,但不能保存下一次加載頁面的選擇。喜歡的東西:
params.php
if (user clicked on "btntheme") {
$theme = 'green.css';
}else{
$theme = "blue.css";
};
的index.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');
});
這改變了風格,但僅適用於當前會話。我怎麼能告訴params.php
,以後$theme
是green.css
,而不是blue.css
?
我treid,它的工作原理,但我看到所有的元素首先用'blue.css'(大約一兩秒)的樣式,然後出現'green.css'。也許這不會發生使用Cookie,而是? – bonaca
這可能是因爲您使用PHP來回應主題。爲什麼不在JS中處理它。PHP在頁面後加載並運行。 – TheValyreanGroup