我使用數(shù)字作為元素 id,在 js 中由 i 表示,一旦按下開始按鈕,它應(yīng)該隱藏標(biāo)題屏幕,更新 i,然后從新的 i 元素中刪除隱藏類(隱藏元素)(問題1)和菜單欄元素。問題 1 和菜單欄應(yīng)留在屏幕上。但是,菜單欄加載,但問題沒有,并彈出錯(cuò)誤。無法讀取未定義的屬性“添加”// variablesvar i = 0;var id = document.getElementById(i.toString());var next = document.getElementById("next");// hiding menu and other screensmenuBar.classList.add('hide');next.classList.add('hide');document.getElementById("1").setAttribute("class", "hide");document.getElementById("2").setAttribute("class", "hide");document.getElementById("3").setAttribute("class", "hide");document.getElementById("4").setAttribute("class", "hide");document.getElementById("5").setAttribute("class", "hide");// show the next screen when the next button is pressedfunction nextScreen() { id.classList.add('hide'); if (i == 0) { menuBar.classList.remove('hide'); } i ++; next.classlist.add('hide'); id.classList.remove('hide');}start.onclick = nextScreen;next.onclick = nextScreen; <div class="container"> <!-- title screen --> <div id='0' class='title'> <h1> Welcome to<br>the<br><strong>IMPOSSIBLE QUIZ</strong> </h1> <button id='start' class='green button'> START </button> </div> <!-- question 1 --> <div id='1' class='question'> <span class='questionIndicator'> Question 1 </span> <br> <h2> What is after </h2> <button id='secretAnswer' class='secret'> ...? </button> <button id='q1WrongAnswer' class='answer button'> this? </button> <button id='q1WrongAnswer' class='answer button'> the answer </button> <button id='q1WrongAnswer' class='answer button'> no idea </button> </div>
更新控制 html 的變量時(shí)無法讀取未定義錯(cuò)誤的屬性“添加”
qq_笑_17
2022-06-09 11:08:07