我们如何根据星期几来加载不同的样式表。我已经编写了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">');
我的样式表位于项目文件夹中名为“样式”的文件夹中。有人可以帮助我。
答案 0 :(得分:2)
您只是将文件附加到正文,而不是通知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 );
如果可能,请确保在您的正文标记之前加载,以防止出现名为FOUC
的问题如果这解决了您的问题,请告诉我。
答案 1 :(得分:1)
由于问题标有asp.net
,为什么不以编程方式执行代码。使用此方法,当浏览器加载sylesheet时,它已经在页面上。
//create a new htmllink
HtmlLink link = new HtmlLink();
//set the correct attributes
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("type", "text/css");
//set the correct stylesheet url based on the weekday name
link.Href = "/Styles/" + DateTime.Now.DayOfWeek + ".css";
//add the htmllink to the page header
this.Page.Header.Controls.Add(link);