我为我的网站制作了一个js代码,但我真的不知道它是正确的,因为它没有任何作用...
代码如下:
<script>
function siteversion() {
var version = getCookie("version");
if (version !="dark") {
root.style.setProperty('--main-bg', "#2d2d2d")
} else {
root.style.setProperty('--main-bg', " url(https://images.unsplash.com/photo-1517524285303-d6fc683dddf8?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80)")
}
}
window.onload = siteversion;
</script>
我希望它会得到一个cookie,该cookie是我制作的,它需要在网站上放置深色或浅色版本。 但是...即使我尝试将Cookie设置为“深色”,当我刷新页面并将其保持在浅色版本时,它也无济于事。
<script>
var colors = ["#2d2d2d", "#f2f2f2"];
var colorIndex = 0;
function changeColor() {
var col = document.getElementById("body");
if (colorIndex >= colors.length) {
colorIndex = 0;
}
col.style.backgroundColor = colors[colorIndex];
colorIndex++;
// the js script to change the bg color
}
function backgroundHiding() {
console.log('backgroundHiding');
let root = document.documentElement;
var rootElement = document.querySelector(':root');
var rootStyles = getComputedStyle(rootElement);
var mainColor = rootStyles.getPropertyValue('--main-bg');
console.log(mainColor); // '#ffeead'
if (mainColor == "#2d2d2d") {
root.style.setProperty('--main-bg', " url(https://images.unsplash.com/photo-1517524285303-d6fc683dddf8?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80)")
document.cookie = "version= ; expires = Thu, 01 Jan 1970 00:00:00 GMT"
document.cookie = "version=light; path=/;"
} else {
root.style.setProperty('--main-bg', "#2d2d2d")
document.cookie = "version= ; expires = Thu, 01 Jan 1970 00:00:00 GMT"
document.cookie = "version=dark; path=/;"
}
// the js script to hide my img bg
}
</script>
[编辑] 拜托,我只有14岁,想学习编程,例如,我一周前才开始编程...
以下是要求的图片: The Console Log
对不起,但是StackOverflow要我建立图像链接,我不能直接放置图像...