2015-03-25 42 views
-1

请查看我的代码。我尝试了很多,但我找不到问题。我如何防止将来出现这样的错误?获取'无法读取null属性样式'

<script> 
var tbox= document.getElementById('tbox'); 
var selector= document.getElementById('selector'); 
function change(tothis){ 
    tbox.style.color=tothis; 
} 
function appear(){ 
    selector.style.visibility='visible'; 
    tbox.focus(); 
} 
function disappear(){ 
    selector.style.visibility='hidden'; 
} 
setTimeout(disappear,500); 
</script> 
+1

看起来像元素不存在 – 2015-03-25 10:21:23

回答

1

错误意味着或者tboxselector不存在。

document.getElementById('tbox');正在寻找一个元件与TBOX的ID:

<p id="tbox"></p>

检查一个元件存在。如果它确实查看代码及其在文档上的位置。如果脚本在head中,那么元素将不会存在。

确保您的代码在dom中的html元素之后或使用doc ready块