弄花香满衣
2016-10-17 10:22
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定时器</title>
<script type="text/javascript">
function clock(){
var mydate=new Date();
atime=mydate.getHours()+":"+mydate.getMinutes()+":"+mydate.getSeconds()
document.write(atime);
document.getElementById("clock").value = atime;
}
setInterval(clock, 1000);
</script>
</head>
<body>
<form>
<input type="button" value="点击我啊" onclick="clock()" />
</form>
</body>
</html>
你改成这样子:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定时器</title>
<script type="text/javascript">
function showTime(){
function clock(){
var mydate=new Date();
atime=('0'+mydate.getHours()).slice(-2)+":"+('0'+mydate.getMinutes()).slice(-2)+":"+('0'+mydate.getSeconds()).slice(-2)
document.getElementById("clock").innerHTML = atime;
}
setInterval(clock, 1000);
}
</script>
</head>
<body>
<form>
<input type="button" value="点击我啊" onclick="showTime()" />
<p id="clock"></p>
</form>
</body>
</html>
你可以参考本节举例中的样式来写;你的input标签里应该是设置有误;不是设置为按钮,要设置为text以显示动态时间
setInterval(clock, 1000);放进方法里面
JavaScript进阶篇
468194 学习 · 21891 问题
相似问题
回答 1