Javascript 动态注释CSS样式表?

Javascript 动态注释CSS样式表?,javascript,css,Javascript,Css,我已经为一个网站建立了相当重的本地存储的明暗模式。 从一个切换到另一个是无缝的,两个看起来都很棒。 然而,在黑暗模式下导航时,我遇到了一些闪烁的问题 在my header.php中,两个样式表都按此顺序向上排列 <link id="light" rel="stylesheet" type="text/css" title="day" href="https://example.com/public/css/main.css"> <link id="dark"

我已经为一个网站建立了相当重的本地存储的明暗模式。 从一个切换到另一个是无缝的,两个看起来都很棒。 然而,在黑暗模式下导航时,我遇到了一些闪烁的问题

在my header.php中,两个样式表都按此顺序向上排列


    <link id="light" rel="stylesheet" type="text/css" title="day" href="https://example.com/public/css/main.css">
    <link id="dark" rel="stylesheet" type="text/css" title="night" href="https://example.com/public/css/night.css">

localstorage开关在同一文件中使用javascript向下处理

在尝试了一百万件事情之后,结果证明,当我注释出上面提到的第一个样式表时,闪烁消失了,浏览是无缝的

因此,我的问题是:如何使用一些添加到localstorage部分的javascript命令注释掉第一个样式表行?或者有没有其他方法可以动态注释该样式表

这里是localstorage函数,我需要它在其中:

<script>
document.getElementById("light").href = localStorage.getItem('light');
document.getElementById("dark").href = localStorage.getItem('dark');

function dayoff() {
document.getElementById("light").href = 'https://example.com/public/css/night.css';
    localStorage.setItem( 'light', 'https://example.com/public/css/night.css' );
document.getElementById("light").href = 'https://example.com/public/css/night.css';
    localStorage.setItem( 'dark', 'https://example.com/public/css/night.css' );
document.getElementById("logo").src = 'https://example.com/public/img/toplogo-night.png';
    location.reload();
}
</script>

document.getElementById(“light”).href=localStorage.getItem(“light”);
document.getElementById(“深色”).href=localStorage.getItem(“深色”);
函数dayoff(){
document.getElementById(“light”).href='https://example.com/public/css/night.css';
setItem('light','https://example.com/public/css/night.css' );
document.getElementById(“light”).href='https://example.com/public/css/night.css';
setItem('dark','https://example.com/public/css/night.css' );
document.getElementById(“logo”).src='10〕https://example.com/public/img/toplogo-night.png';
location.reload();
}

您应该保持用户使用的模式(亮或暗)

然后,您应该根据用户使用的模式添加样式表。

你的职能

function dayoff() {
   // toggle function 
   if(localStorage.getItem("mode") === null || localStorage.getItem("mode") == "light"){
       localStorage.setItem("mode", "dark");
   }
   else{
       localStorage.setItem("mode", "light");
   }
   location.reload();
}
if(localStorage.getItem("mode") === null){
   // first time visitors
    $('head').append('<link id="light" rel="stylesheet" type="text/css" href="https://example.com/public/css/main.css">');
}
else{
   // returning users
   if(localStorage.getItem('mode') == "light"){
      $('head').append('<link id="light" rel="stylesheet" type="text/css" href="https://example.com/public/css/main.css">');
   }
   else{
      $('head').append('<link id="dark" rel="stylesheet" type="text/css" href="https://example.com/public/css/dark.css">');
   }
}
函数dayoff(){
//切换功能
if(localStorage.getItem(“模式”)==null | | localStorage.getItem(“模式”)==light){
setItem(“mode”、“dark”);
}
否则{
setItem(“模式”、“灯光”);
}
location.reload();
}
if(localStorage.getItem(“模式”)==null){
//首次访客
$('head')。追加('');
}
否则{
//返回用户
if(localStorage.getItem('mode')=“light”){
$('head')。追加('');
}
否则{
$('head')。追加('');
}
}

您应该保持用户使用的模式(亮或暗)

然后,您应该根据用户使用的模式添加样式表。

你的职能

function dayoff() {
   // toggle function 
   if(localStorage.getItem("mode") === null || localStorage.getItem("mode") == "light"){
       localStorage.setItem("mode", "dark");
   }
   else{
       localStorage.setItem("mode", "light");
   }
   location.reload();
}
if(localStorage.getItem("mode") === null){
   // first time visitors
    $('head').append('<link id="light" rel="stylesheet" type="text/css" href="https://example.com/public/css/main.css">');
}
else{
   // returning users
   if(localStorage.getItem('mode') == "light"){
      $('head').append('<link id="light" rel="stylesheet" type="text/css" href="https://example.com/public/css/main.css">');
   }
   else{
      $('head').append('<link id="dark" rel="stylesheet" type="text/css" href="https://example.com/public/css/dark.css">');
   }
}
函数dayoff(){
//切换功能
if(localStorage.getItem(“模式”)==null | | localStorage.getItem(“模式”)==light){
setItem(“mode”、“dark”);
}
否则{
setItem(“模式”、“灯光”);
}
location.reload();
}
if(localStorage.getItem(“模式”)==null){
//首次访客
$('head')。追加('');
}
否则{
//返回用户
if(localStorage.getItem('mode')=“light”){
$('head')。追加('');
}
否则{
$('head')。追加('');
}
}

由于它是jquery,我可以将它添加到处理本地存储的javascript函数中吗?让我在我的问题中添加我用来“关灯”的函数。我假设您使用js localstorage?然后是的,但需要有一个标准模式(最好是灯光模式)供首次来访者使用。其中一个或另一个应处于活动状态。如果没有设置样式,网站会看起来很奇怪,不是吗?顺便说一句,谢谢你的时间。检查我更新的答案,这就是你想要的吗?太棒了,只是解析它。你愿意和我一起在聊天室聊几分钟吗?这件事对我来说真的很重要。我可以向您展示整个过程的具体情况。由于它是jquery,我可以将它添加到处理本地存储的javascript函数中吗?让我在我的问题中添加我用来“关灯”的函数。我假设您使用js localstorage?然后是的,但需要有一个标准模式(最好是灯光模式)供首次来访者使用。其中一个或另一个应处于活动状态。如果没有设置样式,网站会看起来很奇怪,不是吗?顺便说一句,谢谢你的时间。检查我更新的答案,这就是你想要的吗?太棒了,只是解析它。你愿意和我一起在聊天室聊几分钟吗?这件事对我来说真的很重要。我可以告诉你整个事情是什么样子的。我会在服务器端这样做。默认情况下,用户有一些样式,而另一个样式甚至不存在于HTML标记上。用户选择另一种风格,然后从PHP中保留一个cookie(数据库字段会更好)并更改CSS。您能展示您的完整功能吗?当然,就这样。我会在服务器端这样做。默认情况下,用户有一些样式,而另一个样式甚至不存在于HTML标记上。用户选择另一种样式,然后从PHP中保留一个cookie(数据库字段会更好)并更改CSS。您能显示完整的功能吗?当然,就这样。