[英]im trying to make a quiz for my bootcamp and i cant figure out what is wrong with the code
我正在嘗試對 class 進行測試,但我似乎找不到問題所在。
`功能詢問問題(隨機){
var random = math.floor(math.random()*testQuestions.length);
var start = document.getElementById(testStart);
start.addEventListner('click', askQuestions);
if(start){
document.write(random);
} 返回; }`
這是 function,我有一個對象數組,在“隨機”變量中引用。 我完全迷失了,我知道我錯過了一些簡單的東西。 這個函數的目的是顯示數組“testQuestions”中的一個隨機問題。
數學是一個全局 object,它以大寫 M 開頭
Math.floor
您正在 function 中添加事件偵聽器。 function 只有在你調用它時才會運行,所以永遠不會添加事件監聽器。 此外, document.write()
將覆蓋整個頁面。 我將其更改為使用document.createElement()
和document.body.appendChild()
。 將您的代碼更改為:
function askQuestions(random) {
var random = Math.floor(Math.random() * testQuestions.length);
var el = document.createElement('p');
el.textContent = random.toString();
document.body.appendChild(el);
}
var start = document.getElementById(testStart);
start.addEventListner('click', askQuestions);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.