JavaScript怎样实现打开页面自动开始倒计时??
如下是代码,点击开始按钮开始两分钟倒计时。请问怎样修改能实现“点开页面自动开始倒计时”?<html><head><metahttp-equiv="Content-Type...
如下是代码,点击开始按钮开始两分钟倒计时。请问怎样修改能实现“点开页面自动开始倒计时”?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="gb2312" />
<title>javascript实现的倒计时时钟</title>
<script language="javascript">
window.onload = function ()
{
var oCountDown = document.getElementById("countdown");
var aInput = oCountDown.getElementsByTagName("input")[0];
var timer = null;
aInput.onclick = function ()
{
this.className == "" ? (timer = setInterval(updateTime, 1000), updateTime()) : (clearInterval(timer));
this.className = this.className == '' ? "cancel" : '';
};
function format(a)
{
return a.toString().replace(/^(\d)$/,'0$1');
}
function updateTime ()
{
var aSpan = oCountDown.getElementsByTagName("span");
var oRemain = aSpan[0].innerHTML.replace(/^0/,'') * 60 + parseInt(aSpan[1].innerHTML.replace(/^0/,''));
if(oRemain <= 0)
{
clearInterval(timer);
return;
}
oRemain--;
aSpan[0].innerHTML = format(parseInt(oRemain / 60));
oRemain %= 60;
aSpan[1].innerHTML = format(parseInt(oRemain));
}
}
</script>
</head>
<body>
<div align="center" id="countdown">
<span>02</span>分钟<span>00</span>秒
<br>
<input type="button" value="开始" />
</div>
</body>
</html>
需要去掉“开始按钮”! 展开
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="gb2312" />
<title>javascript实现的倒计时时钟</title>
<script language="javascript">
window.onload = function ()
{
var oCountDown = document.getElementById("countdown");
var aInput = oCountDown.getElementsByTagName("input")[0];
var timer = null;
aInput.onclick = function ()
{
this.className == "" ? (timer = setInterval(updateTime, 1000), updateTime()) : (clearInterval(timer));
this.className = this.className == '' ? "cancel" : '';
};
function format(a)
{
return a.toString().replace(/^(\d)$/,'0$1');
}
function updateTime ()
{
var aSpan = oCountDown.getElementsByTagName("span");
var oRemain = aSpan[0].innerHTML.replace(/^0/,'') * 60 + parseInt(aSpan[1].innerHTML.replace(/^0/,''));
if(oRemain <= 0)
{
clearInterval(timer);
return;
}
oRemain--;
aSpan[0].innerHTML = format(parseInt(oRemain / 60));
oRemain %= 60;
aSpan[1].innerHTML = format(parseInt(oRemain));
}
}
</script>
</head>
<body>
<div align="center" id="countdown">
<span>02</span>分钟<span>00</span>秒
<br>
<input type="button" value="开始" />
</div>
</body>
</html>
需要去掉“开始按钮”! 展开
1个回答
展开全部
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="gb2312" />
<title>javascript实现的倒计时时钟</title>
<script language="javascript">
window.onload = function ()
{
var oCountDown = document.getElementById("countdown");
var aInput = oCountDown.getElementsByTagName("input")[0];
var timer = null;
aInput.onclick = function ()
{
this.className == "" ? (timer = setInterval(updateTime, 1000), updateTime()) : (clearInterval(timer));
this.className = this.className == '' ? "cancel" : '';
};
function format(a)
{
return a.toString().replace(/^(\d)$/,'0$1');
}
function updateTime ()
{
var aSpan = oCountDown.getElementsByTagName("span");
var oRemain = aSpan[0].innerHTML.replace(/^0/,'') * 60 + parseInt(aSpan[1].innerHTML.replace(/^0/,''));
if(oRemain <= 0)
{
clearInterval(timer);
return;
}
oRemain--;
aSpan[0].innerHTML = format(parseInt(oRemain / 60));
oRemain %= 60;
aSpan[1].innerHTML = format(parseInt(oRemain));
}
// 加上此句即可。
aInput.click();
}
</script>
</head>
<body>
<div align="center" id="countdown">
<span>02</span>分钟<span>00</span>秒
<br>
<input type="button" value="开始" />
</div>
</body>
</html>
追问
这样的话“开始”按钮还是存在呀
能不能把“开始”按钮隐藏或者去掉之后实现自动倒计时?
追答
aInput.style.display="none"
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询