我的页面出现错误,我相信是因为 php if 语句导致条件表未显示。
错误:类型错误:document.getElementById("count2") 为空 源文件:https://www.mysite.com/js/clocktimer.js 行:43
我有一个条件语句:
<?php if ( $this->session->userdata('days_to_challenge') > 0 ) : ?>
<table id="mytable" border="0">
<tr>
<td align="center" colspan="4"><div class="numbers" id="count2" style="padding: 10px; "></div></td>
</tr>
<?php endif; ?>
我的 js 代码导致错误:
document.getElementById('count2').style.display="block";
document.getElementById('count2').style.width="390px";
...
挑战天数已变为零,即。挑战已经开始,所以我没有显示倒计时表。
防止此类错误的最佳方法是什么?
最佳答案
在尝试摆弄它之前测试该元素是否存在。
var count2 = document.getElementById('count2');
if (count2) {
count2.style.display="block";
count2.style.width="390px";
}
关于php - 错误 : TypeError: document. getElementById ("count2") 为空,因为条件表。怎么修?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15219599/