【问题标题】:simple CSS switcher and save cookie简单的 CSS 切换器和保存 cookie
【发布时间】:2016-03-22 11:52:31
【问题描述】:

我想创建一个可以在样式表之间切换的按钮(下面的示例),我希望它创建一个 cookie,以便浏览器记住并保存设置。

<button id="style1">Light</button>
<button id="style2">Dark</button>


<link rel="stylesheet" type="text/css" href="style1.css">
<link rel="stylesheet" type="text/css" href="style1.css">

代码越简单越好。谢谢!

【问题讨论】:

  • 那不可能..
  • @Dsafds 然后在答案中解释。不确定是您还是其他人,但没有必要对该问题投反对票;这个问题很清楚。我们不能指望提问者完全理解解决方案的含义,否则他们不会提问。
  • 您需要为此使用 PHP

标签: html css cookies


【解决方案1】:

您可以使用 PHP 设置和获取 cookie 并根据它更改样式。

switch1.php

<?php
setcookie("s", "1");
header("Location: index.html");
?>

switch2.php

<?php
setcookie("s", "2");
header("Location: index.html");
?>

index.html

<link rel="stylesheet" type="text/css" href=
<?php 
if isset($_COOKIE["s"]))
    echo '"style' . $_COOKIE["s"] . '.css"';
else
    echo '"style1.css"';
?>
>
<button id="style1"><a href="switch1.php">Light</a></button>
<button id="style2"><a href="switch2.php">Dark</a></button>

【讨论】:

  • 我试过了,但似乎无法正常工作。有没有更简单的方法可能是 jquery 或 javascript?另外请注意,我正在尝试将其与 wordpress 一起使用。
猜你喜欢
  • 2016-10-20
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 2019-02-11
  • 1970-01-01
  • 2015-08-30
  • 2014-08-13
相关资源
最近更新 更多