0

我希望根据日期(季节)加载不同的 css 文件。

我尝试在 stackoverflow 上从此处修改图像脚本,但这不起作用。

谁能指出我哪里出错了?

<link href="/css_season/default.css" rel="stylesheet" type="text/css" onload="logo(this)">
function logo(link) {
  var d = new Date();
  var Today = d.getDate();
  var Month = d.getMonth();
  var src;
  if (Month === 4 && (Today >= 1 && Today <= 30)) {
    src = "/css_season/easter.css";
  } else if (Month === 7 && (Today >= 1 && Today <= 31)) {
    src = "/css_season/vacation.css";
  } else if ((Month === 8 && Today >= 30) || (Month === 0 && Today <= 2)) {
    src = "/css_season/vacation.css";
  } else if (Month === 12 && (Today >= 15 && Today <= 31)) {
    src = "/css_season/holidays.css";
  } 
  link.href=href;
}
4

2 回答 2

1

您在 js 函数的末尾有错误的分配。
link.href=href;应该link.href = src;

干杯

于 2012-12-28T10:14:12.583 回答
1

您的分配link.href=href将不起作用,因为href未定义。另外,我会将logo函数放入<body onload="logo();">并为您的link标签提供 id 属性。

这应该适合你:

<html>
<head>
<link id="cssId" href="/css_season/default.css" rel="stylesheet" type="text/css"/>
</head>
<body onload="logo();">
 <!-- body content here -->
</body>

function logo() {
  var d = new Date();
  var Today = d.getDate();
  var Month = d.getMonth();
  var src;
  if (Month === 4 && (Today >= 1 && Today <= 30))
    src = "/css_season/easter.css"; 
  else if (Month === 7 && (Today >= 1 && Today <= 31))
    src = "/css_season/vacation.css";
  else if ((Month === 8 && Today >= 30) || (Month === 0 && Today <= 2))
    src = "/css_season/vacation.css";
  else if (Month === 12 && (Today >= 15 && Today <= 31))
    src = "/css_season/holidays.css";

  document.getElementById('cssId').href=src;
}
于 2012-12-28T10:17:21.440 回答