我想使用像 HH MM SS
这样的表单元素来显示时钟,但它不起作用。
我该怎么做?
<html>
<body>
<p>A script on this page starts this clock:</p>
<p id="demo"></p>
<script>
var myVar = setInterval(myTimer, 1000);
function myTimer() {
var d = new Date();
var t = d.getHours();
var y=d.getMinutes();
var z=d.getSeconds();
document.getElementById("demo").innerHTML = t+":"+y+":"+z;
}
</script>
<script>
function fun1() {
var d = new Date();
var t = d.getHours();
var y=d.getMinutes();
var z=d.getSeconds();
window.document.my.hh.value=t+;
window.document.my.mm.value=+y+;
window.document.my.ss.value=+z;
}
</script>
<form name="my">
HH
<input type="text" name="hh">
MM
<input type="text" name="mm">
SS
<input type="text" name="ss">
</form>
</body>
</html>
我期望输出像 此页面上的脚本启动此时钟:
13:10:24
HH 13 MM 10 SS 24
但实际输出是 此页面上的脚本启动此时钟:
13:10:24
HH MM SS
最佳答案
你应该在那里更改你的代码:
window.document.my.hh.value=t;
window.document.my.mm.value=y;
window.document.my.ss.value=z;
这是我修复的演示:https://codepen.io/phuongnm153/pen/PrQvGq
更新:
问题有点变化。
然后我将 fun1();
添加到 myTimer() 函数
我更新了演示链接
关于javascript - 使用表单元素显示时钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56831738/