【发布时间】:2013-04-19 19:52:52
【问题描述】:
下面是我的javascript,
<script type="text/javascript">
document.getElementById('auth').onclick=change;
function change(){
this.className="account_holder";
}
</script>
html是
<td id="auth" class="authorised_reportericon" >
<a href="{% url incident.views.authorised_reporter %}">
<p align="center" style="color:black;font-size:16px;">Authorised Reporters</p></a>
</td>
我为 onclick 分配了 td 的 id,但它抛出错误为 Uncaught TypeError: Cannot set property 'onclick' of null
【问题讨论】:
-
发生这种情况是因为 ID 为“auth”的元素不存在。为什么?因为 DOM 尚未完全加载以了解元素。使用
window.onload = function() {}并把你的逻辑放在那里
标签: javascript html