Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在a前面加上“;0“;到小于10的数字?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 在a前面加上“;0“;到小于10的数字?

Javascript 在a前面加上“;0“;到小于10的数字?,javascript,jquery,html,Javascript,Jquery,Html,我有一个用HTML和Javascript制作的倒计时钟。当前,当它加载时,由于具有动态宽度,文本将在屏幕上抖动。这是因为我试图强制它显示在页面的中心。我正在寻找一种方法来强制值始终为两位数。是否有一种方法(通过类似if-else语句的方式)可以在所有小于10的值之前预加0?我试着自己实现一些东西,但没有成功,所以我把它注释掉了。代码如下: <!DOCTYPE html> <html> <style> body { background: #ffffff

我有一个用HTML和Javascript制作的倒计时钟。当前,当它加载时,由于具有动态宽度,文本将在屏幕上抖动。这是因为我试图强制它显示在页面的中心。我正在寻找一种方法来强制值始终为两位数。是否有一种方法(通过类似if-else语句的方式)可以在所有小于10的值之前预加0?我试着自己实现一些东西,但没有成功,所以我把它注释掉了。代码如下:

<!DOCTYPE html>
<html>
<style>
body {
    background: #ffffff url("http://i.imgur.com/MelSn4S.png") no-repeat left top;
}
@font-face {
   font-family: sensation;
   src: url(arial);
}
div {
    color: white;
    font-family: arial;
    font-size: 60px;
    position: absolute;
    top: 50%;
    margin-left: 50%;
    margin-right: -50%;
    transform: translate(-50%, -50%)
}
</style>
<head>
<script type="text/javascript">
    var tenYearsInSeconds = 315360000;
    var yearInSeconds = 31536000;
    var monthInSeconds = 2592000;
    var dayInSeconds = 84600;
    var hourInSeconds = 3600;
    var minuteInSeconds = 60;

    var timer = window.setInterval(function() {countdown()}, 10);
    var runonce = true;

function countdown() {
    var temp = tenYearsInSeconds;
    var years = Math.floor(temp/yearInSeconds);

    temp = temp % yearInSeconds;
    var months = Math.floor(temp/monthInSeconds);

    temp = temp % monthInSeconds;
    var days = Math.floor(temp/dayInSeconds);

    temp = temp % dayInSeconds;
    var hours = Math.floor(temp/hourInSeconds);

    temp = temp % hourInSeconds;
    var minutes = Math.floor(temp/minuteInSeconds);


    document.getElementById('time').innerHTML = 
        "Years: " + years + 
        " Months: " + months + 
        " Days: " + days + 
        " Hours: " + hours + 
        " Minutes: " + minutes;
    tenYearsInSeconds = tenYearsInSeconds - 30000;

    if (tenYearsInSeconds <=0 && runonce) {
        tenYearsInSeconds = 0;
        runonce = false;
        countdown();
        clearInterval(timer);
    }

//if (years < 10) {
   //years = "0" + years;
//} 

}
</script>
</head>

<body>
<script type="text/javascript">
    countdown();
</script>
<div id="time"></div>
</body>
</html>

身体{
背景:#ffffff url(“http://i.imgur.com/MelSn4S.png)无重复左上;
}
@字体{
字体家族:感觉;
src:url(arial);
}
div{
颜色:白色;
字体系列:arial;
字体大小:60px;
位置:绝对位置;
最高:50%;
左边距:50%;
保证金权利:-50%;
转换:转换(-50%,-50%)
}
var tenYearsInSeconds=315360000;
var YearUnseconds=31536000;
var Monthinses=2592000;
var dayInSeconds=84600;
var hourInSeconds=3600;
var分钟秒=60;
var timer=window.setInterval(函数(){countdown()},10);
var runonce=真;
函数倒计时(){
var temp=十年零秒;
变量年数=数学下限(温度/年数);
温度=温度%YearUnseconds;
变量月份=数学下限(温度/月秒);
温度=温度%月秒;
变量天数=数学下限(温度/天数);
温度=温度%dayinds;
var小时=数学地板(温度/小时/秒);
温度=温度%小时秒;
var分钟=数学地板(温度/分钟秒);
document.getElementById('time')。innerHTML=
“年:“+年+
“月数:“+月数+
“天:“+天+
“小时数:“+Hours+
“分钟:”+分钟;
十年每秒=十年每秒-30000;
如果(十年1秒
while(number.toString().length
把它放在前面

   while (number.toString().length < desiredLength) {
      number = "0".concat(number);
   }
while(number.toString().length
尽管泰勒使用了
append
,但他真正的意思是
prepend
有没有一种方法可以在所有小于10的值之前追加一个0
)。感谢您的关注,我编辑了这个问题以反映更改。请参阅
格式()
函数。请确保在显示值之前进行格式化(在
文档.getElementById('time').innerHTML=…
语句中完成)。根据注释掉的代码的位置,看起来您是在之后进行的。感谢大家的所有注释。这很有帮助。
number=(number<10)?“0”+数字:数字;
如果数字小于10,则添加前面的0。否则不必麻烦。
   while (number.toString().length < desiredLength) {
      number = "0".concat(number);
   }