本文介绍了添加“0”如果时钟有一位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些时钟脚本。一切都很好,它的工作完美,但......我有一个问题。如果在时钟设置一位数小时或分钟,如1:5时钟之前不添加0数字。这就是我所做的,但它不起作用。你可以帮助我吗?

i have some clock script. Everything is fine and it's work perfectly but... i have one problem. If at the clock is set one digit hour or minute like 1:5 clock not adding "0" digit before. This what i'v done but it does't work. Can u help me, much thx?

window.setInterval(function update_clock() {
    var currentTime = new Date();
    var currentHours = currentTime.getHours();
    var currentMinutes = currentTime.getMinutes();
    $.ajax({
        success: function (clock) {
            document.getElementById("hour").firstChild.nodeValue = currentHours;
            document.getElementById("minutes").firstChild.nodeValue = currentMinutes;
            if (currentMinutes.length == 1) {
                currentMinutes = "0" + currentMinutes;
            }
        }
    });
}, 999);


推荐答案

currentMinutes 是一个数字,因此它没有 length 属性。此外,在将 currentMinutes 设置为分钟长度 $ c>元素。

currentMinutes is a number, so it does not have the length property. Also, you must check the length before set the currentMinutes to the minutes element.

类似于:

var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();

$.ajax({
    success: function (clock) {
        if (currentMinutes.toString().length == 1) {
            currentMinutes = "0" + currentMinutes;
        }

        document.getElementById("hour").firstChild.nodeValue = currentHours;
        document.getElementById("minutes").firstChild.nodeValue = currentMinutes;
    }
});

这篇关于添加“0”如果时钟有一位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 10:53