<分区>
标签 javascript jquery html css
<分区>
我想得到秒的长度。如果秒是一个字符而不是加 0 否则什么都不做。 这是我的代码
<p>Click the button to display the seconds of the time right now.</p>
<p id="demo"></p>
<script>
var d = new Date();
var n = d.getSeconds();
var z = n.length;
document.getElementById("demo").innerHTML = z;
</script>
最佳答案
getSeconds 函数返回一个没有长度属性的数字。
如果您想获取数字中有多少个字符,您可以使用 .toString() 将其转换为字符串
<p>Click the button to display the seconds of the time right now.</p>
<p id="demo"></p>
<script>
var d = new Date();
var n = d.getSeconds();
var z = n.toString().length;
document.getElementById("demo").innerHTML = z;
</script>
关于javascript - 为什么这个长度函数未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58748933/