我如何使出现2个数字在分钟和秒,并删除旁边的定时器文本
How do I make appear 2 digits in minutes and seconds and remove text that is beside the timer?
我试图用JavaScript制作一个倒计时计时器,但有两件事我不能改变,这是两个数字,应该像这样显示&;01:00&;,但相反,它看起来像&;1:9&;和一个短语,除了显示定时器&;这只对下一个&;有效。我想删除它,但是失败了。
<html>
<head>
<title>Countdown</title>
<script type="text/javascript">
// set minutes
var mins = 20;
// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;
function countdown() {
setTimeout('Decrement()',1000);
}
function Decrement() {
if (document.getElementById) {
minutes = document.getElementById("minutes");
seconds = document.getElementById("seconds");
// if less than a minute remaining
if (seconds < 59) {
seconds.value = secs;
} else {
minutes.value = getminutes();
seconds.value = getseconds();
}
secs--;
setTimeout('Decrement()',1000);
}
}
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return mins;
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return secs-Math.round(mins *60);
}
</script>
</head>
<body>
<div id="timer">
This is only valid for the next <input id="minutes" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;"> : <input id="seconds" type="text" style="width: 60px; border: none; background-color:none; font-size: 50px; font-weight: bold;">
</div>
<script>
countdown();
try this
function getminutes() {
// minutes is seconds divided by 60, rounded down
mins = Math.floor(secs / 60);
return ("0" + mins).substr(-2);
}
function getseconds() {
// take mins remaining (as seconds) away from total seconds remaining
return ("0" + (secs-Math.round(mins *60))).substr(-2);
}
填充0只需:
var renderedSecs;
renderedSecs = (secs < 10 ? "0" : "") + secs;
// now put renderedSecs into the HTML
不要对secs
这样做,因为它是number
,而renderedSecs
是string
。
保持你计算的实际数字与它们的呈现(作为字符串)分开。
相关文章:
- 单击文本时删除文本框的默认数据
- 使用删除文本创建新行
- 为什么jQuery正在删除文本的最后一个字符
- 在验证之前从输入值中删除文本并提交
- 如何使用jquery从父元素中删除文本(不删除内部元素)
- 删除文本区域中某个文本后出现的换行符
- javascript:删除文本中选定元素的标记
- <tspan>文本</tspan>如果没有定义id或类,如何删除文本
- 从网站中删除文本选择限制的脚本
- 如何防止用户删除文本输入中的前三个字符
- 粘贴文本时删除文本区域中的字符
- 如何在文本输入中插入删除图标并实现删除文本
- 在 Ckeditor 中,删除文本前后的 br 标签
- 如何在javascript中使用动态ID删除文本框
- 如何删除文本节点
- Javascript 焦点删除文本突出显示
- 添加或删除文本框,然后使用 PHP 或 jquery 获取它们的值
- 动态删除文本框
- 当处于活动状态时,我如何删除文本修饰.我有以下代码,但链接带有下划线
- 使用 Javascript 添加文本框后动态删除文本框