javascript动态时钟 IE中不能动态显示,而在谷歌浏览器中却能,下面是代码
<head><metacontent="text/html;charset=utf-8"http-equiv="Content-Type"/><title>无标题1</t...
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>无标题 1</title>
<script language="javascript" >
var timerID=null;
var timeRunning=false;
function stopclock()
{
if(timeRunning)
{
clearTimeout(timerID);
}
timeRunning=false;
}
function showtime()
{
now=new Date();
var hours=now.getHours();
var minutes=now.getMinutes();
var seconds=now.getSeconds();
var timeValue=" "+(hours>12)? hours-12:hours;//这一行也有问题,12点时输出的是0点,也顺便指点一下吧
timeValue+=((minutes<10)? ":0" : ":")+minutes;
timeValue+=((seconds<10)? ":0" : ":")+seconds;
timeValue+=(hours>=12)? " P.M" : " A.M";
document.open();
document.write("<font size=:'12pt' face='Times New Roman'><B>当前时间:");
document.write(timeValue);
document.close();
timerID=setTimeout("showtime()",1000);
timeRunning=true;
}
function startclock()
{
stopclock();
showtime();
}
</script>
</head>
<body onload="startclock()">
</body> 展开
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>无标题 1</title>
<script language="javascript" >
var timerID=null;
var timeRunning=false;
function stopclock()
{
if(timeRunning)
{
clearTimeout(timerID);
}
timeRunning=false;
}
function showtime()
{
now=new Date();
var hours=now.getHours();
var minutes=now.getMinutes();
var seconds=now.getSeconds();
var timeValue=" "+(hours>12)? hours-12:hours;//这一行也有问题,12点时输出的是0点,也顺便指点一下吧
timeValue+=((minutes<10)? ":0" : ":")+minutes;
timeValue+=((seconds<10)? ":0" : ":")+seconds;
timeValue+=(hours>=12)? " P.M" : " A.M";
document.open();
document.write("<font size=:'12pt' face='Times New Roman'><B>当前时间:");
document.write(timeValue);
document.close();
timerID=setTimeout("showtime()",1000);
timeRunning=true;
}
function startclock()
{
stopclock();
showtime();
}
</script>
</head>
<body onload="startclock()">
</body> 展开
2个回答
展开全部
好像IE不支持document.open()/document.close()
另外,setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式
不需要通过setTimeout来调用自身,这部分代码在FF下也有问题,因为在函数内部调用自己,当自身还没有声明完成时,会有问题的。
时间哪部分没有问题呀,你用下面的测试代码试一试:
var hours = 12;
alert((hours > 12) ? hours - 12 : hours);
下面是时钟部分的代码
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title></title>
<script language="javascript" type="text/javascript">
function showtime() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var timeValue = " " + (hours > 12) ? hours - 12 : hours;
timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
timeValue += (hours >= 12) ? " P.M" : " A.M";
document.getElementById("Time").innerHTML = "<font size=:'12pt' face='Times New Roman'><B>Time:" + timeValue + "</B></font>";
}
function startclock() {
showtime();
}
setInterval("showtime()", 1000);
</script>
</head>
<body onload="startclock()">
<div id="Time"></div>
</body>
</html>
另外,setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式
不需要通过setTimeout来调用自身,这部分代码在FF下也有问题,因为在函数内部调用自己,当自身还没有声明完成时,会有问题的。
时间哪部分没有问题呀,你用下面的测试代码试一试:
var hours = 12;
alert((hours > 12) ? hours - 12 : hours);
下面是时钟部分的代码
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title></title>
<script language="javascript" type="text/javascript">
function showtime() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var timeValue = " " + (hours > 12) ? hours - 12 : hours;
timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
timeValue += (hours >= 12) ? " P.M" : " A.M";
document.getElementById("Time").innerHTML = "<font size=:'12pt' face='Times New Roman'><B>Time:" + timeValue + "</B></font>";
}
function startclock() {
showtime();
}
setInterval("showtime()", 1000);
</script>
</head>
<body onload="startclock()">
<div id="Time"></div>
</body>
</html>
展开全部
给你一段 我写的和你想要结果一样 的代码
<html>
<head>
<script type="text/javascript">
window.onload=function ()
{
setInterval(startTime,500);
startTime();}
function startTime()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
m=checkTime(m)
s=checkTime(s)
if(h>12){h-=12;document.getElementById('txt').innerHTML=h+":"+m+":"+s+" PM";}
else
document.getElementById('txt').innerHTML=h+":"+m+":"+s+" AM";}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
</script>
</head>
<body >
<div id="txt"></div>
</body>
</html>
<html>
<head>
<script type="text/javascript">
window.onload=function ()
{
setInterval(startTime,500);
startTime();}
function startTime()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
m=checkTime(m)
s=checkTime(s)
if(h>12){h-=12;document.getElementById('txt').innerHTML=h+":"+m+":"+s+" PM";}
else
document.getElementById('txt').innerHTML=h+":"+m+":"+s+" AM";}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
</script>
</head>
<body >
<div id="txt"></div>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询