ReferenceError:未定义文档(在纯JavaScript中) [英] ReferenceError: document is not defined (in plain JavaScript)

查看:140
本文介绍了ReferenceError:未定义文档(在纯JavaScript中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试

var body = document.getElementsByTagName("body")[0];

我之前在其他代码中看过这个并没有造成任何麻烦。为什么现在呢?
随附的HTML页面只是正文中的一个div。

I have seen this before in others code and didn't cause any trouble. Why is it now? The companied HTML page is just a div inside the body.

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="css/quiz.css" />
    <script type="text/javascript" src="js/quiz.js"></script>
</head>
<body>

    <div id="divid">Next</div>

</body>
</html>

代码如下:

(function(){
        var body = document.getElementsByTagName("body")[0];

        function Question(question, choices, correctAns) {
            this.question = question;
            this.choices = choices;
            this.correctAns = correctAns;
        }

        Question.prototype.checkAns = function(givenAns){
            if (this.correctAns === givenAns) {
                console.log("OK");
            }
        };

        function Quiz() {
            this.questions = [];
        }

        Quiz.prototype.showAllQuestions = function(){
            this.questions.forEach(function(questions){
                console.log(questions.question);
            });
        };

        Quiz.prototype.showQuiz = function(){
            this.questions.forEach(function(questions){

                for (var i=0; i < questions.choices.length; i+=1) {
                    body.innerHTML(
                            "<input type=\"radio\" name=\"sex\" value=\"male\">" 
                            + questions.choices[i] + "<br>");
                }

            });
        };

        var q1 = new Question("What is red?", ["Color","Animal","Building"],1);
        var q2 = new Question("Most popular music?", ["Latin","Pop","Rock"],2);
        var quiz = new Quiz();

        quiz.questions.push(q1);
        quiz.questions.push(q2);
        quiz.showAllQuestions();


        })();

尝试此链接中的完整代码这里

Try the whole code in this link HERE

推荐答案

这取决于何时自我执行匿名函数正在运行。它可能在 window.document 定义之前运行。

It depends on when the self executing anonymous function is running. It is possible that it is running before window.document is defined.

在这种情况下,尝试添加一个监听器

In that case, try adding a listener

window.addEventListener('load', yourFunction, false);
// ..... or 
window.addEventListener('DOMContentLoaded', yourFunction, false);

yourFunction () {
  // some ocde

}

更新:(更新问题并包含代码后)

Update: (after the update of the question and inclusion of the code)

阅读以下有关从插入的JavaScript引用DOM元素并在 head 元素中运行时出现问题:

- getElementsByTagName(...)[0]未定义?

- 遍历DOM

Read the following about the issues in referencing DOM elements from a JavaScript inserted and run in head element:
- "getElementsByTagName(…)[0]" is undefined?
- Traversing the DOM

这篇关于ReferenceError:未定义文档(在纯JavaScript中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆