2015-03-25 41 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塊