简体   繁体   English

Javascript-使用函数来验证div的内容是否与其他内容匹配?

[英]Javascript - Using a function to verify a div's content matches other content?

I'm working on a small maths game to help me self-learn Javascript and I'm currently stuck with getting a function to work. 我正在开发一个小型数学游戏,以帮助我自学Javascript,而我目前仍然无法使用某个功能。 The general idea is that when the player clicks on one of the four possible answers, the function checkAnswer() will verify that the content of the selected div matches the correct answer. 总体思路是,当玩家单击四个可能答案之一时, function checkAnswer()将验证所选div的内容是否与正确答案匹配。 But because there are four different divs, I can't find a way to get it to verify this unless I created four separate functions, one for each button of each div. 但是因为有四个不同的div,所以除非我创建了四个单独的函数(每个div的每个按钮一个),否则我找不到找到一种方法来验证这一点。 And even then, the 'correct answer' it needs to be compared to is generated and nested inside another function (generateQuestion) because my friend said if I place it outside with the other variables, it will create the same question each time. 即使这样,仍然需要生成与之比较的“正确答案”,并将其嵌套在另一个函数(generateQuestion)中,因为我的朋友说,如果我将其与其他变量放在一起,则每次都会创建相同的问题。 What code is used for this check feature? 此检查功能使用什么代码? All of the code for the game is in the JSbin, but this is the Javascript that I'm having trouble with: 游戏的所有代码都在JSbin中,但这是我遇到的Javascript:

function answerCheck(clicked_id) {
  if (clicked_id == document.getElementById("question").innerHTML) {
    document.getElementById("correct").style.display = "block";
  }

}

http://jsbin.com/nunusarofo/edit?html,css,js,output http://jsbin.com/nunusarofo/edit?html,css,js,输出

Thank you. 谢谢。

What you're trying to figure out is a way to connect an html element that represents an answer to some internal data corresponding to the same answer. 您试图找出的是一种将html元素连接起来的方法,该html元素表示与相同答案对应的一些内部数据的答案。

The approach you have is a good start, each answer tries to use a unique value to identify itself: 您采用的方法是一个好的开始,每个答案都尝试使用唯一的值来标识自己:

<div id="choices">
  <div id="box1" class="box"><button id="answer1" onclick="answerCheck(1)"></button></div>
  <div id="box2" class="box"><button id="answer2" onclick="answerCheck(2)"></button></div>
  <div id="box3" class="box"><button id="answer3" onclick="answerCheck(3)"></button></div>
  <div id="box4" class="box"><button id="answer4" onclick="answerCheck(4)"></button></div>
</div>

But the problem now becomes how to use that value to obtain the actual answer data? 但是现在的问题变成了如何使用该值来获取实际答案数据?

Well, you're already keeping track of correctIndex that will allow you to extract the correct answer like so answerSelection[correctIndex] . 好吧,您已经在跟踪correctIndex ,它将使您能够像answerSelection[correctIndex]这样提取正确的答案。 Why not, instead of arbitrary passing 1,2,3,4 for the answers, you pass in their indexes 0,1,2,3 : 为什么不这样做,而不是随意传递1,2,3,4作为答案,而是传递其索引0,1,2,3

<div id="choices">
  <div id="box1" class="box"><button id="answer1" onclick="answerCheck(0)"></button></div>
  <div id="box2" class="box"><button id="answer2" onclick="answerCheck(1)"></button></div>
  <div id="box3" class="box"><button id="answer3" onclick="answerCheck(2)"></button></div>
  <div id="box4" class="box"><button id="answer4" onclick="answerCheck(3)"></button></div>
</div>

and compare if the given index matches the correctIndex : 并比较给定的索引是否匹配正确的correctIndex

function answerCheck(clicked_index) {
  if (clicked_index === correctIndex) {
    document.getElementById("correct").style.display = "block";
  }
}

I found a few issues with the jsBin code. 我发现jsBin代码存在一些问题。

For the choice boxes, you have buttons with the onclick handler for the answerCheck . 对于选择框,您具有带有answerCheckonclick处理程序的answerCheck When the game starts, the button is replaced with the answer options. 游戏开始时,该按钮将替换为答案选项。 The button with the click handler is gone. 带有点击处理程序的按钮不见了。

These remove the buttons with onclick : 这些通过onclick删除按钮:

document.getElementById("box1").innerHTML = answerSelection[0];
document.getElementById("box2").innerHTML = answerSelection[1];
document.getElementById("box3").innerHTML = answerSelection[2];
document.getElementById("box4").innerHTML = answerSelection[3];

This can be fixed by moving onclick up one level in the dom and removing the button all together since the game logic does that anyways: 可以通过以下方法解决此问题:将dom上的onclick向上移动一个级别,然后一起删除按钮,因为游戏逻辑总是这样做:

<div id="box1" class="box"><button id="answer1" onclick="answerCheck(1)"></button></div>
<div id="box2" class="box"><button id="answer2" onclick="answerCheck(2)"></button></div>
<div id="box3" class="box"><button id="answer3" onclick="answerCheck(3)"></button></div>
<div id="box4" class="box"><button id="answer4" onclick="answerCheck(4)"></button></div>

to

<div id="box1" class="box" onclick="answerCheck(0)"></div>
<div id="box2" class="box" onclick="answerCheck(1)"></div>
<div id="box3" class="box" onclick="answerCheck(2)"></div>
<div id="box4" class="box" onclick="answerCheck(3)"></div>

I also would change the index for answerCheck to start with 0 which would also match the logic in your code. 我还将更改answerCheck的索引以0开头,这也将与您代码中的逻辑匹配。

Then, to verify the correct answer, I would compare the index of the correct answer to the clicked index as well as if the game is running: 然后,要验证正确答案,我将比较正确答案的索引与单击的索引以及游戏是否正在运行:

function answerCheck(clicked_id) {
    if(isPlaying && clicked_id == correctIndex) {
        document.getElementById("correct").style.display = "block";
    }      
}

From there...you should be able to continue whatever game logic you had going! 从那里...您应该能够继续进行任何游戏逻辑!

Answer here: http://jsbin.com/vojeyivobe/1/edit?html,js,console,output 在这里回答: http : //jsbin.com/vojeyivobe/1/edit?html,js,console,output

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM