慕粉1474278593
2018-12-19 10:15
<script type="text/javascript">
setInterval(clock,1000);
var attime;
function clock(){
var time=new Date();
var hour = time.getHours()<10?"0"+time.getHours():time.getHours();
var minute = time.getMinutes()<10?"0"+time.getMinutes():time.getMinutes();
var second = time.getSeconds()<10?"0"+time.getSeconds():time.getSeconds();
attime= hour+"时:"+minute+"分:"+second+"秒";
document.getElementById("clock").value = attime;
}
</script>
明白了谢谢
比如早上九点零五分,你可以写成标准的09:05,保证都是两位数
time.getHours()<10?"0"+time.getHours():time.getHours();
请问这小于10是什么意思啊
<!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();
attime=time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
document.getElementById("clock").value = attime;
}
setInterval(clock,1000);
</script>
</head>
<body>
<form>
<input type="text" id="clock" size="50" style="background:#000;color:#00ff00;width:50px"; />
</form>
</body>
</html>
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题