Javascript TypeError:divOutput为空

Javascript TypeError:divOutput为空,javascript,ecmascript-6,Javascript,Ecmascript 6,JavaScript代码中的html变量在我的问题循环通过页面后不会显示在屏幕上。我得到这个错误消息,TypeError:divOutput为null。在我所有的问题循环通过之后 这是HTML <!DOCTYPE html> <html> <head> <title>JavaScript ES2015 Practice</title> <script type="text/javascript" src="js

JavaScript代码中的html变量在我的问题循环通过页面后不会显示在屏幕上。我得到这个错误消息,TypeError:divOutput为null。在我所有的问题循环通过之后

这是HTML

 <!DOCTYPE html>
 <html>
<head>
    <title>JavaScript ES2015 Practice</title>
    <script type="text/javascript" src="js/output.js"></script>
</head>
<body>
    <div id="text">
    </div>
</body>
Javascript代码

var quizQuestions;
var quizAnswers;
var correctAnswers;
var wrongAnswers;
var html;
var response;

 const questions = [

{
    question:"What is the captial of Georgia [type quit to quit]",
      answer:"atlanta"
},
{
    question:"What is the captial of New York [type quit to quit]",
      answer:"albany"
},
{
    question:"What is the captial of Texas [type quit to quit]",
      answer:"austin"
}
];

 const print = (message) =>{
 let divOutput = document.getElementById("text");
    divOutput.innerHTML = message; 
  }

        for(let i = 0; i < questions.length; i++){

            quizQuestions = questions[i].question;
            quizAnswers = questions[i].answer;
            response = prompt(quizQuestions);

                if(response === quizAnswers){
                    correctAnswers++;
                }else{
                    wrongAnswers++;
                }   

    html = `You got  ${correctAnswers}  questions right`;  //this doesn't display to the screen

        }   // end of for loop


      print(html);
在onload回调中移动脚本,以便脚本在

文档中的所有对象和所有图像都位于DOM中 子框架已完成加载

然后就可以得到输出了

脚本应为:

    window.onload = function () {
        var quizQuestions;
        var quizAnswers;
        var correctAnswers = 0;
        var wrongAnswers = 0;
        var html;
        var response;

        const questions = [

            {
                question: "What is the captial of Georgia [type quit to quit]",
                answer: "atlanta"
            },
            {
                question: "What is the captial of New York [type quit to quit]",
                answer: "albany"
            },
            {
                question: "What is the captial of Texas [type quit to quit]",
                answer: "austin"
            }
        ];

        const print = (message) => {
            let divOutput = document.getElementById("text");
            divOutput.innerHTML = message;
        }

        for (let i = 0; i < questions.length; i++) {

            quizQuestions = questions[i].question;
            quizAnswers = questions[i].answer;
            response = prompt(quizQuestions);

            if (response === quizAnswers) {
                correctAnswers++;
            } else {
                wrongAnswers++;
            }

            html = `You got  ${correctAnswers}  questions right`;  //this doesn't display to the screen

        }   // end of for loop


        print(html);
    }

为了让它更合法地工作

@myko,如果这样做有效,你可以接受答案并投票结束这个问题
    window.onload = function () {
        var quizQuestions;
        var quizAnswers;
        var correctAnswers = 0;
        var wrongAnswers = 0;
        var html;
        var response;

        const questions = [

            {
                question: "What is the captial of Georgia [type quit to quit]",
                answer: "atlanta"
            },
            {
                question: "What is the captial of New York [type quit to quit]",
                answer: "albany"
            },
            {
                question: "What is the captial of Texas [type quit to quit]",
                answer: "austin"
            }
        ];

        const print = (message) => {
            let divOutput = document.getElementById("text");
            divOutput.innerHTML = message;
        }

        for (let i = 0; i < questions.length; i++) {

            quizQuestions = questions[i].question;
            quizAnswers = questions[i].answer;
            response = prompt(quizQuestions);

            if (response === quizAnswers) {
                correctAnswers++;
            } else {
                wrongAnswers++;
            }

            html = `You got  ${correctAnswers}  questions right`;  //this doesn't display to the screen

        }   // end of for loop


        print(html);
    }
        var correctAnswers = 0;
        var wrongAnswers = 0;