javascript - 为什么我的文本更新函数不显示文本?

标签 javascript html css

为什么我的textUpdated()功能不工作?

我想在容器 ( <div id = 'textUpdate'> ) 下显示文本,但没有显示任何内容。

如果您想查看完整代码:https://repl.it/@securethebags/theBeatles-quiz

运行它: https://thebeatles-quiz.securethebags.repl.co/

我试过改变我的 if-else-if 背后的逻辑声明,但这没有用。我还检查以确保我所有的 var 名称都是正确的:

function selectAnswer(e){  //called when an answer button is pressed
    clearStatusClass(document.body)
    const selectedButton = e.target;
    const correct = selectedButton.dataset.correct
    Array.from(answerButtonsElement.children).forEach(button =>{
        setStatusClass(button, button.dataset.correct) // change 
        background for each button if answer is correct/wrong
    })
    if(shuffledQuestions.length > currentQuestionIndex + 1){
        nextButton.classList.remove('hide')
    }
    else{
        startButton.innerText = "Restart"
        startButton.classList.remove('hide')
    }
    updateScore(correct);
    textUpdatd() 
}

function textUpdatd(){
    textUpdate.classList.remove('hide')
    if(scoreboard.right == questions.length)
        textUpdate.innerText = "WOW you got them all right! Groovy!"
    else if(scoreboard.wrong == questions.length)
        textUpdate.innerText = "WOW you got them all wrong! Gnarly!"
    else if(scoreboard.right > scoreboard.wrong)
        textUpdate.innerText = "Not bad! - You must know your stuff!"
    else if(scoreboard.wrong > scoreboard.right )
        textUpdate.innerText = "Not doing too hot! Better luck next time!"
    else if(scoreboard.wrong == scoreboard.right && scoreboard.right != 0 || scoreboard.wrong != 0)
        textUpdate.innerText = "You are currently tied with right and wrong answers!"
    else
        textUpdate.innerText = "Something must of went wrong!" 
}

最佳答案

你需要从你的函数中返回。向您的函数添加返回语句。

我发现您在调用函数后忘记了分号 ;

此外,您可以通过按 F12 并打开控制台选项卡来检查您的页面以查看错误是什么。

关于javascript - 为什么我的文本更新函数不显示文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58612129/

相关文章:

javascript - 负载在 jquery 中不起作用,请帮忙

javascript - 将 HTML 插入到 Google 表格的弹出窗口或注释中

javascript - 无法降低html中图像的高度

css - 图像上缩进的透明箭头/三 Angular 形

html - 如何在 Angular 7 上延迟显示点击表单?

html - Dart 交互/访问数据库

元素的 Javascript 选择器/非 CSS 选择器

javascript - 使用嵌套对象验证 Backbone 模型

javascript - 异步循环

javascript - Redux 状态改变后怎么办?