【发布时间】:2014-05-02 20:58:01
【问题描述】:
您好,我正在尝试从数组中返回一个随机单词。一切正常,但屏幕上打印的问题有没有办法在命令提示框中显示数组中的随机单词?
这是我的代码,它可以工作,但问题出现在屏幕上而不是控制台中。有什么办法吗??
var NorthAmericaQuestions = ["How many States are in USA? Are There 51, 52, 54, 50?","How big are Dictionaries? Huge, Okayish, Thin?"]
var NorthAmericaAnswers = [52,"Huge"]
function RandomGen() {
randno = Math.floor ( Math.random() * NorthAmericaQuestions.length );
document.write(NorthAmericaQuestions[randno]);
}
Q1 = prompt (RandomGen(), "Type Here!");
if (Q1 == NorthAmericaAnswers[0]){
document.write("<br>Thats Correct!!");
}
else {
document.write ("<br>Oops Your Wrong!!");
}
【问题讨论】:
-
console.log(NorthAmericaQuestions[randno])不是document.write(NorthAmericaQuestions[randno]);? -
嗨,感谢您的帮助,但是当放置 console.log 而不是 document.write 时,它不会出现在网页上,这很好,但我如何让它出现在提示框中他们回答问题???
-
你不能使用这个方法。如果您想弹出答案,请改用
alert。alert('Answer correct!!')。否则考虑使用分层的divs 来产生你想要的效果。
标签: javascript arrays dom console document