我遇到了一个在网络上找到的执行倒计时的代码的小问题:
取消注释 var 分钟和秒它可以工作,但是如果我尝试通过 getElementById 获取分钟和秒变量,则代码不再工作.. 为什么?有什么建议可以纠正这种行为吗?
<html>
<head>
<script>
var interval;
//var minutes = 0;
//var seconds = 2;
window.onload = function() {
var minutes = document.getElementById("idtextmin").value;
var seconds = document.getElementById("idtextsec").value;
countdown('countdown');
}
function countdown(element) {
interval = setInterval(function() {
var el = document.getElementById(element);
if(seconds == 0) {
if(minutes == 0) {
el.innerHTML = "countdown's over!";
clearInterval(interval);
return;
} else {
minutes--;
seconds = 60;
}
}
if(minutes > 0) {
var minute_text = minutes + (minutes > 1 ? ' minutes' : ' minute');
} else {
var minute_text = '';
}
var second_text = seconds > 1 ? 'seconds' : 'second';
el.innerHTML = minute_text + ' ' + seconds + ' ' + second_text + ' remaining';
seconds--;
}, 1000);
}
</script>
</head>
<body>
<form>
Min<input type="text" id="idtextmin" name="textmin" value="2" />
Sec<input type="text" id="idtextsec" name="textsec" value="9" />
</form>
<div id='countdown'></div>
</body>
</html>