gpt4 book ai didi

javascript - 无法根据星期几加载样式表

转载 作者:行者123 更新时间:2023-11-30 11:41:15 25 4
gpt4 key购买 nike

我们如何根据星期几加载不同的样式表。我写了 js,但它不起作用。

  var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var n = weekday[d.getDay()];
document.writeln('<link rel="stylesheet" type="text/css" href="../Styles/' + n +'.css">');

我的样式表位于项目文件夹中名为“Styles”的文件夹中。有人能帮帮我吗。

最佳答案

您只是将您的文件附加到正文,而不是通知 DOM 有一个新的样式表文件。

试试这个:

var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var n = weekday[d.getDay()];


var link = document.createElement( "link" );
link.href = "../Styles/" + n +".css";
link.type = "text/css";
link.rel = "stylesheet";
link.media = "screen,print";

document.getElementsByTagName( "head" )[0].appendChild( link );

如果可能,请确保它在您的 body 标签之前加载,以防止称为 FOUC 的问题

如果这解决了您的问题,请告诉我。

关于javascript - 无法根据星期几加载样式表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42575430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com