我的博客是用的hexo的next主题,但是老是报如下的错误:
对应的代码:
1 2
| document.getElementById("timeDate").innerHTML = "本站已安全运行 "+dnum+" 天 "; document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
|
报错信息:找不到元素,无法进行innerHTML操作
错误原因可能有两个:
- js引用了当前js下面的标签元素,如果js代码在引用的标签元素之上,是引用不了其中的元素的,应当将其置于对应的元素的下面
- 还有一种情况就是,页面中根本没有这个元素,解决办法自然就是创建对应的元素咯,打开博客根目录下的themes\next\layout_partials,添加如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <span id="timeDate">站点运行天数</span> <span id="times">运行的时分秒</span>
<script> var now = new Date(); function createtime() { var grt= new Date("05/20/2020 12:00:00"); now.setTime(now.getTime()+250); days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days); hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours); if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum); mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;} seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum); snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;} document.getElementById("timeDate").innerHTML = "本站已安全运行 "+dnum+" 天 "; document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒"; } setInterval("createtime()",250); </script>
|