[英]When the javascript is within the html it works when it outside it doesn't
I use the following javascript .. when it is written inside the html file it works 当它写在html文件中时,我使用以下javascript ..
<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>
It works perfectly fine, but when I created a js file copied it all (exact same just without the script tag 它工作正常,但是当我创建一个js文件时,将其全部复制(完全相同,只是没有script标签
then called the file like so 然后像这样调用文件
<script type="text/javascript" src="js/showdate.js" ></script>
it simply doesn't work 它根本不起作用
Thanks for whoever is willing to help 谢谢愿意帮助的人
<script language="Javascript" type="text/javascript" src="js/showdate.js"></script>
try to write this line of code inside the <head>
tags. 尝试将这行代码写入<head>
标记内。 Then add <body onload="showdate();">
so that the function is called everytime the page loads. 然后添加<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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.