簡體   English   中英

Javascript 對象值問題未定義

[英]Javascript object value problem undefined

您好開發人員 我在 javascript 中遇到了對象屬性問題,我將一些對象放入數組中,但我無法從該數組中獲取值,它僅表示未定義 對此有任何解決方案嗎? 我在這里嘗試了一些方法,但沒有奏效
非常感謝,如果有任何幫助我

 const quiz = [ { q1: "Where Are You", ans1 : "Alaska", ans2: "Alabama", } , { q1: "Where Are You", ans1: "Alaska", ans2: "Alabama", } ] console.log(quiz.q1); let loadquiz = document.querySelector(".quiz"); let span =document.querySelector("span"); span.innerText = quiz.q1;
 body { background-color: #141C35; padding: 0; margin: 0; display: flex; width: 100%; min-height: 100vh; align-items: center; color: white; justify-content: center; flex-direction: column; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; } /* #212134*/ .quiz { background-color:#4259FA; width: 500px; color: white; min-height: 300px; display: flex; flex-direction: column; align-items: center; padding:50px 0; justify-content: center; } .quiz h2 { padding: 10px; } .quiz span { padding: 15px; margin: 5px; }
 <body> <h1>Quiz App</h1> <div class="quiz"> <h2> Where do you live ?</h2> <span>Question goes here</span> <span>Question goes here</span> <span>Question goes here</span> </div> </body>

您有一個對象列表(例如 [] )(例如 {}),要訪問該對象的任何鍵,您必須首先轉到該對象的索引

 const quiz = [ { q1: "Where Are You", ans1 : "Alaska", ans2: "Alabama", } , { q1: "Where Are You", ans1: "Alaska", ans2: "Alabama", } ] console.log(quiz[0].q1); let loadquiz = document.querySelector(".quiz"); let span =document.querySelector("span"); span.innerText = quiz[0].q1;
 body { background-color: #141C35; padding: 0; margin: 0; display: flex; width: 100%; min-height: 100vh; align-items: center; color: white; justify-content: center; flex-direction: column; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; } /* #212134*/ .quiz { background-color:#4259FA; width: 500px; color: white; min-height: 300px; display: flex; flex-direction: column; align-items: center; padding:50px 0; justify-content: center; } .quiz h2 { padding: 10px; } .quiz span { padding: 15px; margin: 5px; }
 <body> <h1>Quiz App</h1> <div class="quiz"> <h2> Where do you live ?</h2> <span>Question goes here</span> <span>Question goes here</span> <span>Question goes here</span> </div> </body>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM