我使用以下javascript ..当它在html文件中写入时它可以工作
<script type="text/javascript">
var d=new Date();
var ShowenYear = d.getFullYear();
var Month = d.getMonth();
var NextSeason = "Unknown Season";
if (Month == 1 || Month == 2 || Month == 3) {
NextSeason = "Spring";
}
else if (Month == 4 || Month == 5 || Month == 6) {
NextSeason = "Summer";
}
else if (Month == 7 || Month == 8 || Month == 9) {
NextSeason = "Autumn";
}
else if (Month == 10 || Month == 11 || Month == 12) {
NextSeason = "Winter";
ShowenYear++;
}
document.getElementById("Season").innerHTML = NextSeason + " " + ShowenYear;
</script>
它工作得非常好,但是当我创建一个js文件时将其全部复制(完全相同,只是没有脚本标记
然后像这样调用文件
<script type="text/javascript" src="js/showdate.js" ></script>
它根本不起作用
感谢愿意提供帮助的人
答案 0 :(得分:1)
<script language="Javascript" type="text/javascript" src="js/showdate.js"></script>
尝试在<head>
标记内写下这行代码。然后添加<body onload="showdate();">
,以便每次加载页面时调用该函数。
function showdate(){
var d=new Date();
var ShowenYear = d.getFullYear();
var Month = d.getMonth();
var NextSeason = "Unknown Season";
if (Month == 1 || Month == 2 || Month == 3) {
NextSeason = "Spring";
}
else if (Month == 4 || Month == 5 || Month == 6) {
NextSeason = "Summer";
}
else if (Month == 7 || Month == 8 || Month == 9) {
NextSeason = "Autumn";
}
else if (Month == 10 || Month == 11 || Month == 12) {
NextSeason = "Winter";
ShowenYear++;
}
document.getElementById("Season").innerHTML = NextSeason + " " + ShowenYear;
}
}