【发布时间】:2021-04-06 16:17:48
【问题描述】:
我是 JS 新手,我想就如何使用 Javascript 和我在下面定义的对象加载页面 DOM 后如何填充下拉菜单寻求指导。我在这里和其他地方看了几个例子;但是,我仍然不清楚如何做到这一点。在下面的代码中,我不太明白我应该在 opt.innerHTML 中包含什么?是带有指向对象的变量的 ul li 元素吗?感谢您对此的任何帮助。
const score = document.querySelector('#score');
const output = document.querySelector('#output');
const answerSelect = document.querySelector('#selAnswers');
const categorgySelect = {
Category: 'Sports',
Category: 'Art',
Category: 'Vehicles',
Category: 'History',
};
const difficultySelect = {
Difficulty: 'easy',
Difficulty: 'medium',
Difficulty: 'hard',
};
let categoryPick = document.querySelector('#category');
document.addEventListener('DOMContentLoaded', function() {
for(let index in categorySelect) {
let opt = document.createElement('option');
opt.value = categorySelect;
opt.innerHTML = ???
categorySelect.appendChild(opt);
}
});
<!doctype html>
<html lang="en">
<head>
<title>l</title>
<link rel='stylesheet' href='css/Trivia.css' >
</head>
<body>
<h1>Trivia</h1>
<div>Score: <span id="score">Correct 0 Wrong 0</span></div>
<br>
<div>
<label>Select Category:</label>
<select id='category'></select>
<br>
<label>Select Difficulty:</label>
<select id='difficulty'></select>
</div>
<br><br>
<div id="output"></div>
<br>
<div id="selAnswers"></div>
<br>`enter code here`
<button id="btn">Get First Question!</button>
<script src ='js/Trivia.js'></script>
</body>
</html>
【问题讨论】:
标签: javascript html json object