javascript - 简单的 javascript JS 事件处理程序无法正常工作

标签 javascript event-handling

下面是我的代码及其附带的说明。目前,错误答案的响应文本显示在页面加载时,我无法将其更改为正确答案。

// Declare a string variable called question and set it equal to a True/False question of your choice. 
var question = "KD has sold his legacy at OKC for a championship ring at GS.";

// Declare a boolean variable called answer and set it equal to true or false (the answer to the question you asked above.) 
var answer = true;

// Create a function called loadQuestion() that sets the value of the questionText element equal to the question variable you declared above. 
function loadQuestion(){
document.getElementById("questionText").innerHTML = question;
}

// Create a function called checkAnswer(userAnswer) that takes one incoming parameter. 
function checkAnswer(userAnswer){
  if(userAnswer===answer){
    document.getElementById("responseText").innerHTML = "That is correct!";             
    document.getElementById("responseText").className = "correctAnswer";
  }
  else if(userAnswer!==answer){       
    document.getElementById("responseText").innerHTML = "I'm sorry, that is    
     not correct.";
    document.getElementById("responseText").className = "incorrectAnswer";
  }
}
// ---> If your answer variable matches the incoming userAnswer parameter, write a success message in the element called responseText. 
// ---> If your answer variable does NOT match the userAnswer parameter, write a failure message in the element called responseText. 

// Create TRADITIONAL DOM EVENT HANDLERS for the "onclick" events for the three buttons.
var start = document.getElementById("startButton");
var truth = document.getElementById("trueButton");
var falsify = document.getElementById("falseButton");
// The Start button should trigger the loadQuestion method
start.onclick = loadQuestion;
// The True button should trigger the checkAnswer method with a value of "true"
truth.onclick = checkAnswer("true");
// The False button should trigger the checkAnswer method with a value of "false"
falsify.onclick = checkAnswer("false");

最佳答案

如果您要比较三重相等===,请确保两侧的类型相同

关于javascript - 简单的 javascript JS 事件处理程序无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38250121/

相关文章:

javascript - Cef 中的 OnContextCreated() 未被调用

javascript - 假设我无法使用 Chrome 控制台更改页面 css,我可以记录页面内容并设置样式吗?

javascript - 无法让 Jest 与包含主题的样式化组件一起工作

javascript - Date() 的时间源是什么?

javascript - <script> 中的 Struts html 标记

java - 使用 Apache Felix iPOJO 事件管理处理程序

javascript - (Chrome 扩展) 如何测试 onUpdateAvailable

javascript - 如何为 html 创建 iPhone 联系人标题滚动效果?

c# - 是什么阻止我的事件处理程序方法完成?

java - GWT - 添加可见性 true/false 处理程序