JavaScript 未运行

发布于 2024-10-31 09:03:47 字数 779 浏览 1 评论 0原文

我这里有这段代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽袖吟 2024-11-07 09:03:47

您的函数名为 createQuestions,但您将其调用为 createQuestion。否则语法看起来不错。

此外,如果将此代码立即嵌入到您的页面中,它可能无法运行,因为当您的 for 循环执行时,文档将不完全存在;因此 quiz_section div 将不存在。

将循环包含在如下所示的函数中:

function initializeQuiz() {
    for (i=0;i<4;i++) {
        var elem=document.getElementById('quiz_section')
        var func=createQuestions(i)
        elem.innerHTML=elem.innerHTML+func+"<br />"
    }
}

并将 onload='javascript:initializeQuiz()' 属性添加到 标记中。

Your function is named createQuestions but you are calling it as createQuestion. 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:

function initializeQuiz() {
    for (i=0;i<4;i++) {
        var elem=document.getElementById('quiz_section')
        var func=createQuestions(i)
        elem.innerHTML=elem.innerHTML+func+"<br />"
    }
}

And add an onload='javascript:initializeQuiz()' attribute to your <BODY> tag.

情绪失控 2024-11-07 09:03:47

您定义了createQuestions(),但是您正在调用createQuestion(),这自然会抛出ReferenceError

另外,请在每行后使用分号。 (为了良好的实践)

You define createQuestions(), however you are calling createQuestion(), this will naturally throw a ReferenceError.

Also please use semi-colons after each line. ( For good practice )

小傻瓜 2024-11-07 09:03:47

创建问题应该是创建问题

createQuestion should be createQuestions

帅的被狗咬 2024-11-07 09:03:47

您可以将问题声明调整为:

var Questions = [
  { Question: "What is the answer?", Values: ["A", "B", "C", "D"], Answer: 3 },
  { Question: "What is the answer?", Values: ["A", "B", "C", "D"], Answer: 3 },
  { Question: "What is the answer?", Values: ["A", "B", "C", "D"], Answer: 3 },
  { Question: "What is the answer?", Values: ["A", "B", "C", "D"], Answer: 3 }
];

这样您就可以将每个项目作为对象访问:

var question = Questions[0];
// question.Question
// question.Values
// question.Answer

You could adjust your declaration of your questions to:

var Questions = [
  { Question: "What is the answer?", Values: ["A", "B", "C", "D"], Answer: 3 },
  { Question: "What is the answer?", Values: ["A", "B", "C", "D"], Answer: 3 },
  { Question: "What is the answer?", Values: ["A", "B", "C", "D"], Answer: 3 },
  { Question: "What is the answer?", Values: ["A", "B", "C", "D"], Answer: 3 }
];

That way you can access each item as an object:

var question = Questions[0];
// question.Question
// question.Values
// question.Answer
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文