我的代碼似乎有一個問題,我不知道如何解決。我對這個東西非常陌生,幾乎不知道我在做什麼。每次運行我的代碼時都會彈出此錯誤消息。我試圖讓你點擊藍色框消失的按鈕。當你再次點擊該按鈕時,藍色框再次出現,綠色框消失。這裏是我的代碼:未捕獲TypeError:無法讀取屬性'樣式'null
<script type="text/javascript">
function toggle_visibility(id1,id2){
var e1 = document.getElementById(id1);
var e2 = document.getElementById(id2);
if (e1.style.visibility == 'hidden') {
e1.style.visibility = 'visible';
e2.style.visibility = 'visible';}
else{
e1.style.visibility = 'hidden';
e2.style.visibility = 'hidden';
}
}
</script>
<div class="square" id="bluebox" style="visibility:visible"></div><br>
<div class="box" id="greenbox" style="visibility:visbible"></div><br>
<button onclick=" toggle_visibility(greenbox,bluebox)">Pls Work</button>
謝謝!很棒! –