为什么我的 javascript 时钟不工作?
Why is my javascript clock not working?
正如您在下面看到的,我有一个时钟代码,包括提前 2 周设置的日期,但是它在本地或服务器上都不起作用。
有人可以告诉我我做错了什么吗?
代码也在下面
<script type="text/javascript">
tday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
tmonth=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
function GetClock(){
var d=new Date(+new Date + 12096e5);
var dx=d.toGMTString();
dx=dx.substr(0,dx.length -3);
d.setTime(Date.parse(dx))
d.setSeconds(d.getSeconds() + <?php date_default_timezone_set('Europe/London'); echo date('Z'); ?>);
var nday=d.getDay(),nmonth=d.getMonth(),ndate=d.getDate(),nyear=d.getYear(),nhour=d.getHours(),nmin=d.getMinutes(),nsec=d.getSeconds(),ap;
if(nhour==0){ap=" AM";nhour=12;}
else if(nhour<12){ap=" AM";}
else if(nhour==12){ap=" PM";}
else if(nhour>12){ap=" PM";nhour-=12;}
if(nyear<1000) nyear+=1900;
if(nmin<=9) nmin="0"+nmin;
if(nsec<=9) nsec="0"+nsec;
document.getElementById('clockbox').innerHTML=""+tday[nday]+", "+tmonth[nmonth]+" "+ndate+", "+nyear+" "+nhour+":"+nmin+":"+nsec+ap+"";
}
window.onload=function(){
GetClock();
setInterval(GetClock,1000);
}
</script>
<div id="clockbox"></div>
您的页面有错误:
Uncaught SyntaxError: Unexpected token <
因为PHP
<?php date_default_timezone_set('Europe/London'); echo date('Z'); ?>
不在 运行 文件中 .html
。您必须将扩展名更改为 .php
并且您的服务器必须支持 PHP.
And 正如@Oriol 指出的那样,作为替代方案,您可以将服务器配置为将 .html
作为 PHP
文件处理。
为了回答您的问题,它不起作用,因为服务器没有处理您的 php 代码,因为它是一个 .html 页面。
在此处查看如何修复:
How do I add PHP code/file to HTML(.html) files?
正如您在下面看到的,我有一个时钟代码,包括提前 2 周设置的日期,但是它在本地或服务器上都不起作用。
有人可以告诉我我做错了什么吗?
代码也在下面
<script type="text/javascript">
tday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
tmonth=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
function GetClock(){
var d=new Date(+new Date + 12096e5);
var dx=d.toGMTString();
dx=dx.substr(0,dx.length -3);
d.setTime(Date.parse(dx))
d.setSeconds(d.getSeconds() + <?php date_default_timezone_set('Europe/London'); echo date('Z'); ?>);
var nday=d.getDay(),nmonth=d.getMonth(),ndate=d.getDate(),nyear=d.getYear(),nhour=d.getHours(),nmin=d.getMinutes(),nsec=d.getSeconds(),ap;
if(nhour==0){ap=" AM";nhour=12;}
else if(nhour<12){ap=" AM";}
else if(nhour==12){ap=" PM";}
else if(nhour>12){ap=" PM";nhour-=12;}
if(nyear<1000) nyear+=1900;
if(nmin<=9) nmin="0"+nmin;
if(nsec<=9) nsec="0"+nsec;
document.getElementById('clockbox').innerHTML=""+tday[nday]+", "+tmonth[nmonth]+" "+ndate+", "+nyear+" "+nhour+":"+nmin+":"+nsec+ap+"";
}
window.onload=function(){
GetClock();
setInterval(GetClock,1000);
}
</script>
<div id="clockbox"></div>
您的页面有错误:
Uncaught SyntaxError: Unexpected token <
因为PHP
<?php date_default_timezone_set('Europe/London'); echo date('Z'); ?>
不在 运行 文件中 .html
。您必须将扩展名更改为 .php
并且您的服务器必须支持 PHP.
And 正如@Oriol 指出的那样,作为替代方案,您可以将服务器配置为将 .html
作为 PHP
文件处理。
为了回答您的问题,它不起作用,因为服务器没有处理您的 php 代码,因为它是一个 .html 页面。
在此处查看如何修复:
How do I add PHP code/file to HTML(.html) files?