JavaScript 未运行
我这里有这段代码:
var Questions=[]
Questions[0]=["What is the answer?",["A","B","C","D"],3]
Questions[1]=["What is the answer?",["A","B","C","D"],3]
Questions[2]=["What is the answer?",["A","B","C","D"],3]
Questions[3]=["What is the answer?",["A","B","C","D"],3]
function createQuestions(id) {
var tReturn="<form>"
tReturn=tReturn+"<b>Questions "+id+":</b>"
tReturn=tReturn+"</form>"
return tReturn;
}
for (i=0;i<4;i++) {
var elem=document.getElementById('quiz_section')
var func=createQuestion(i)
elem.innerHTML=elem.innerHTML+func+"<br />"
}
我最近刚刚开始使用 Javascript。我知道这里一定有语法错误,但我找不到它。事实上,主文档中有一个 id 为“quiz_selection”的 DIV。
我无法访问任何类型的调试器,因为我在学校,几乎所有内容都被阻止。
如果可以的话谢谢!
I have this code here:
var Questions=[]
Questions[0]=["What is the answer?",["A","B","C","D"],3]
Questions[1]=["What is the answer?",["A","B","C","D"],3]
Questions[2]=["What is the answer?",["A","B","C","D"],3]
Questions[3]=["What is the answer?",["A","B","C","D"],3]
function createQuestions(id) {
var tReturn="<form>"
tReturn=tReturn+"<b>Questions "+id+":</b>"
tReturn=tReturn+"</form>"
return tReturn;
}
for (i=0;i<4;i++) {
var elem=document.getElementById('quiz_section')
var func=createQuestion(i)
elem.innerHTML=elem.innerHTML+func+"<br />"
}
I just started using Javascript recently. I know there must be a syntax error in here somewhere but I can't find it. There is, in fact a DIV in the main document with an id of "quiz_selection".
I don't have access to any kind of debugger because I'm at school and almost everything is blocked.
Thanks if you can!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的函数名为
createQuestions
,但您将其调用为createQuestion
。否则语法看起来不错。此外,如果将此代码立即嵌入到您的页面中,它可能无法运行,因为当您的 for 循环执行时,文档将不完全存在;因此
quiz_section
div 将不存在。将循环包含在如下所示的函数中:
并将
onload='javascript:initializeQuiz()'
属性添加到标记中。
Your function is named
createQuestions
but you are calling it ascreateQuestion
. Otherwise the syntax seems fine.Additionally, if this code is embedded immediately into your page it may not function because the document will not fully exist when your for loop executes; hence the
quiz_section
div will not exist.Enclose your loop in a function like so:
And add an
onload='javascript:initializeQuiz()'
attribute to your<BODY>
tag.您定义了
createQuestions()
,但是您正在调用createQuestion()
,这自然会抛出ReferenceError
。另外,请在每行后使用分号。 (为了良好的实践)
You define
createQuestions()
, however you are callingcreateQuestion()
, this will naturally throw aReferenceError
.Also please use semi-colons after each line. ( For good practice )
创建问题应该是创建问题
createQuestion should be createQuestions
您可以将问题声明调整为:
这样您就可以将每个项目作为对象访问:
You could adjust your declaration of your questions to:
That way you can access each item as an object: