错误: 无法设置属性“innerHTML”的值: 对象为 null 或未定义script typetext/javascript languageJa
错误: 无法设置属性“innerHTML”的值: 对象为 null 或未定义
<script type="text/javascript"; language="JavaScript">
<!--
var maxtime = 1 * 60 //一个小时,按秒计算
function CountDown() {
if (maxtime >= 0) {
minutes = Math.floor(maxtime / 60);
seconds = Math.floor(maxtime % 60);
msg = "距离结束时间还有" + minutes + "分" + seconds + "秒";
document.getElementById("timer").innerHTML = msg;
if (maxtime == 0.5 * 60) alert('注意,还有30秒!');
--maxtime;
}
else {
clearInterval(timer);
alert("时间到!考试结束");
document.all.BtnSubmit.click();
}
}
timer = setInterval("CountDown()", 1000);
</script>
<div id="timer" style="color:red"> </div>
------解决方案--------------------
新建一个html文档,复制如下代码:
<script>
function CountDown() {
document.getElementById("timer").innerHTML = "123";
}
</script>
<div id="timer" style="color:red"> </div>
<input type=button onclick="CountDown()" value="test"></input>
在我这里可以运行。
[解决办法]var maxtime = 1 * 60 //一个小时,按秒计算
function CountDown() {
if (maxtime >= 0) {
minutes = Math.floor(maxtime / 60);
seconds = Math.floor(maxtime % 60);
msg = "距离结束时间还有" + minutes + "分" + seconds + "秒";
document.getElementById("timer").innerHTML = msg;
if (maxtime == 0.5 * 60){
alert('注意,还有30秒!')
};
--maxtime;
}
else {
clearInterval(timer);
alert("时间到!考试结束");
//document.all.BtnSubmit.click();
}
}
timer = setInterval("CountDown()", 1000);
[解决办法]不你代码放在
window.onload=function()
{
//这里。
}
对象没有加载你就使用了
[解决办法]html解析先后顺序问题。script放到最后执行。或者写个执行方法。在onload执行。如#6楼一样。
[解决办法]帅哥,用label控件不可以吗?
document.getElementById("timer").value= msg;
[解决办法]document.getElementById("timer").innerHTML = msg;
取timer没取到,把脚本放到页脚。你放到上面,页面还没rander完,脚本抓dom元素肯定抓不到。