Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何记住样式更改?_Php_Jquery_Css - Fatal编程技术网

Php 如何记住样式更改?

Php 如何记住样式更改?,php,jquery,css,Php,Jquery,Css,关于cookies和localStorage(设置、删除…)的文章太多了,但我不清楚应该在哪里放置一些代码 也就是说,我试图通过单击按钮来更改css样式表,但无法在下次加载页面时保存该选项。比如: params.php if (user clicked on "btntheme") { $theme = 'green.css'; }else{ $theme = "blue.css"; }; index.php <?php include ("params.ph

关于
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>

这将更改样式,但仅适用于当前会话。如何告诉
params.php
将来
$theme
green.css
,而不是
blue.css

您可以使用
select
元素将
localStorage
设置为用户选择的值。使用
$.holdReady(true)
检查
localStorage
是否为
null
,如果在键处设置了值,请设置
链接的
href
元素,否则调用
$.holdReady(false)
。在用户不进行选择的情况下,考虑设置默认的<代码>代码> HRFF <代码>元素,以供用户不进行选择。< /P> html


您可以使用
select
元素将
localStorage
设置为用户选择的值。使用
$.holdReady(true)
检查
localStorage
是否为
null
,如果在键处设置了值,请设置
链接的
href
元素,否则调用
$.holdReady(false)
。在用户不进行选择的情况下,考虑设置默认的<代码>代码> HRFF <代码>元素,以供用户不进行选择。< /P> html


使用本地存储非常简单

设置

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();
});

您可以在
文档中调用该函数。ready
函数也可以将其设置为页面加载。

使用本地存储非常简单

设置

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();
});

您可以在
文档中调用该函数。就绪
函数也可以将其设置为页面加载。

I treid,它可以工作,但我看到所有元素都是首先由
blue.css
(大约一两秒钟)设置样式,然后出现
green.css
。也许使用cookies不会发生这种情况?这可能是因为您使用PHP来响应主题。为什么不在JS中处理这一切呢。PHP是在页面之后加载和运行的。I treid,它可以工作,但我看到所有元素都是先用
blue.css
(大约一两秒钟)设置样式,然后出现
green.css
。也许使用cookies不会发生这种情况?这可能是因为您使用PHP来响应主题。为什么不在JS中处理这一切呢。PHP是在页面之后加载和运行的。I treid,它可以工作,但我看到所有元素都是先用
blue.css
(大约一两秒钟)设置样式,然后出现
green.css
。使用cookies可能不会发生这种情况?请尝试将
元素后面的
元素替换为
$。holdReady()
以包含
if
语句。或者,如果用户没有在
元素中进行选择,则在
链接中包含一个默认
href
元素,并在
.ready()中设置默认的
href
处理程序。如果用户没有在
元素中进行选择,则它可以工作,但我看到所有元素都首先由
blue.css
设置样式(大约一两秒钟)然后出现
green.css
。使用cookies可能不会发生这种情况?请尝试将
元素后面的
元素替换为
$。holdReady()
以包含
if
语句。或者,如果用户尚未在
元素中进行选择,则在
链接中包含默认的
href
元素,并在
.ready()中设置默认的
href
处理程序。
function changeTheme(){
   var theme = localStorage.getItem('theme');
   $('#link01').attr('href', theme + '.css');
}

$('#btntheme').click(function(){
   localStorage.setItem('theme','green');
   changeTheme();
});