<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定时器</title>
<script type="text/javascript">
var attime;
function clock(){
var time=new Date();
var hour=time.getHours();
var minute=time.getMinutes();
var second=time.getSeconds();
var date=document.write(hour + ":"+minute+":"+second);
var attime=date;
setInterval(clock,100);
document.getElementById("clock").value = attime;
}
</script>
</head>
<body>
<form>
<input type="text" id="clock" size="50" />
</form>
</body>
</html>
有三个问题: 第一个document.write本身是输出 类似一个功能 而你应该是想把时间赋值给date 所以应该是 var date=document.write(hour + ":"+minute+":"+second) ; 第二个问题,源代码中这句var date=document.write(hour + ":"+minute+":"+second); 最后这个分号用的是中文分号; 第三个问题 计时器应该放在函数外面 修改后就能正常运行了:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定时器</title>
<script type="text/javascript">
var attime;
function clock(){
var time=new Date();
var hour=time.getHours();
var minute=time.getMinutes();
var second=time.getSeconds();
var date=(hour + ":"+minute+":"+second);
var attime=date;
document.getElementById("clock").value = attime;
}
setInterval(clock,1000);
</script>
</head>
<body>
<form>
<input type="text" id="clock" size="50" />
</form>
</body>
</html>
没有var date = document.write(hour + ":" + minute + ":" + second) 这种写法吧
<script type="text/javascript">
window.onload = function(){
var attime;
function clock() {
var time = new Date();
var hour = time.getHours();
var minute = time.getMinutes();
var second = time.getSeconds();
//var date = document.write(hour + ":" + minute + ":" + second) ;
var attime = hour + ":" + minute + ":" + second;
document.getElementById("clocks").value = "";
document.getElementById("clocks").value = attime;
setInterval(clock,1000)
}
clock()
}
</script>
没听懂,那是在input里调用吗
clock函数没调用,setInterval 那里有问题,注意一下