JavaScript 无法在应用程序上运行

标签 javascript html

所以我正在尝试按照 this blog post 中的代码构建一个测验应用程序(codepen example)。

我已经完成了所有操作,但 JS 似乎不起作用,因为除了 HTML 和 CSS 之外,没有任何问题、答案或任何内容出现。 Here's my JSFiddle attempt连同代码

window.onload = function () {

    var questionArea = document.getElementsByClassName('questions')[0],
        answerArea   = document.getElementsByClassName('answers')[0],
        checker      = document.getElementsByClassName('checker')[0],
        current      = 0,


        allQuestions = {
            //question and answer list, the number is the index of the answers
            'madrugada' : ['journey', 'dawn', 'mother', 1],
            'manzana' : ['apple', 'insane', 'men', 0],
            'vivir' : ['villain', 'to live', 'to go', 1],
            'amarillo' : ['love', 'river', 'yellow', 2],
            'almendra' : ['almond', 'cheese', 'rails', 0],
            'cascabel' : ['jingle bell', 'helmet', 'beauty', 0],
            'aceituna' : ['tuna', 'oil', 'olive', 2],
            'azar' : ['king', 'chance', 'zebra', 1],
            'milagro' : ['voyage', 'tea', 'miracle', 2],
            'añoranza' : ['snore', 'dusk', 'longing', 2],
            'abecedario' : ['cedar', 'alphabet', 'ability', 1],
            'frenesí' : ['frenzy', 'freaky', 'neat', 0],
            'hechizo' : ['spell', 'done', 'zone', 0],
            'alma' : ['almond', 'soul', 'leaf', 1],
            'mariposa' : ['marriage', 'pose', 'butterfly', 2],
            'siempre' : ['person', 'always', 'simple', 1],
            'anochecer' : ['dusk', 'anual', 'dawn', 0],
            'chiste' : ['clean', 'cheese', 'joke', 2],
            'ojo' : ['eye', 'eight', 'dot', 0],
            'ojalá' : ['eyeball', 'hopefully', 'lullaby', 1]
        };


    function loadQuestion(curr) {
        //Loads questions into question area

        var question = Object.keys(allQuestions)[cur];
        //remove everything in q area and add current question in
        questionArea.innerHTML = '';
        questionArea.innerHTML = question;
    }

    function loadAnswers(curr) {
        //Loads all the possible answers of the given question 

        var answers = allQuestions[Object.keys(allQuestions)[curr]];
        //empty answer area
        answerArea.innerHTML = '';
        //add possible answers to answerArea
        for (var i = 0; i < answers.length - 1; i++) {
            var createDiv = document.createElement('div'),
                text = document.createTextNode(answers[i]);

            createDiv.appendChild(text);
            //adds an onclick function to the answer; a click will execute a function to check if the answer was correct
            createDiv.addEventListener("click", checkAnswer(i, answers));

            answerArea.appendChild(createDiv);
        }
    }

    function checkAnswer(i, arr) {
        //checks if answer given is same as the correct one, and if it is the last question. If it is the last question, it empties answerArea

        return function() {
            var givenAnswer = i,
                correctAnswer = arr[arr.length - 1];

            if (givenAnswer === correctAnswer) {
                addChecker(true);
            } else {
                addChecker(false);
            }

            if (current < Object.keys(allQuestions).length - 1) {
                current++;

                loadQuestion(current);
                loadAnswers(current);
            } else {
                questionArea.innerHTML = '¡Fin!';
                answerArea.innerHTML = '';
            }
        };
    }

    function addChecker(bool) {
        //adds div element to page, used to see whether answer was correct or false

        var createDiv = document.createElement('div'),
            txt       = document.createTextNode(current + 1);

        createDiv.appendChild(txt);

        if (bool) {
            createDiv.className += 'correct';
            checker.appendChild(createDiv);
        } else {
            createDiv.className += 'false';
            checker.appendChild(createDiv);
        }
    }
};

感谢您的帮助!

最佳答案

您没有调用启动和运行所需的函数,而是仅在代码中定义了它们。就这么称呼他们吧...

// Start the quiz right away
loadQuestion(current);
loadAnswers(current);

另外,对于 JSFiddle,不要为 window.onload 烦恼。

<小时/>

JSFiddle Link - 更新的示例

关于JavaScript 无法在应用程序上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38667396/

相关文章:

javascript - 在 JavaScript 中实现撤消功能

javascript - 在 Canvas 中沿贝塞尔曲线移动 Sprite

javascript - 我可以在异步函数的 try/catch block 中使用多个 'await' 吗?

javascript - javascript中从右到左语言环境的连接字符串

javascript - 压缩网页中的 XML

javascript - 获取输入数组的第 n 个键值

javascript - 没有在单选按钮上进行默认检查

javascript - 使用 highcharts.js 在每个 xAxis 类别标签上显示自定义图标图像 [html]

html - 在 Foundation 的全宽行中居中内容

javascript - 捕获phonegap文件上传的文件名-phonegap