javascript类型错误innerHTML

时间:2017-06-08 15:16:31

标签: javascript html

我正在编写一个代码并且我得到一个“未捕获的TypeError:无法设置属性'innerHTML'的null”错误

HTML

<html>
<head>
<script src ="timer.js"></script>
</head>
<body>
<div id="r"></div>
</body>
</html>

Java脚本

//window.onload=function(){
var start=Date.now(),r=document.getElementById('r');
(function f(){
var diff=Date.now()-start,ns=(((3e5-diff)/1000)>>0),m=(ns/60)>>0,s=ns-m*60;
r.textContent="Registration closes in "+m+':'+((''+s).length>1?'':'0')+s+' 
minutes';
document.getElementById('r').innerHTML = result;
if(diff>(3e5)){start=Date.now()}
setTimeout(f,1000);
})();
//}

我只是想在html页面上显示结果

1 个答案:

答案 0 :(得分:1)

没有id =&#39; test&#39;

的元素

我认为你必须写:

(function f() {
    var start = Date.now(),
    var r = document.getElementById('r');
    var diff = Date.now() - start,
        ns = (((3e5 - diff) / 1000) >> 0),
        m = (ns / 60) >> 0,
        s = ns - m * 60;
    r.textContent = "Registration closes in " + m + ':' + (('' + s).length > 1 ? '' : '0') + s + ' minutes';
    //here the change
    document.getElementById('r').innerHTML = result;
    if (diff > (3e5)) {
        start = Date.now()
    }
    setTimeout(f, 1000);
})();

请使用Javascript缩进。