当我点击一个按钮时,会出现相应的答案,而其他答案不应显示

我有十个这样的按钮和答案,每个按钮和答案之间的唯一区别是答案:


例子:


  <div class="showhim"> 

        <button class="answerbtn" onclick="revealAnswersFunction()">Click/Tap To Reveal Answers:</button>

    <div class="showme" style="display:none">1D, 2C</div>

</div>


  <div class="showhim"> 

        <button class="answerbtn" onclick="revealAnswersFunction()">Click/Tap To Reveal Answers:</button>

    <div class="showme" style="display:none">1A, 2D</div>

</div>


  <div class="showhim"> 

        <button class="answerbtn" onclick="revealAnswersFunction()">Click/Tap To Reveal Answers:</button>

    <div class="showme" style="display:none">1B, 2B</div>

</div>

现在我想要的是,如果用户单击一个按钮,就会显示相关的答案。目前,它正在这样做,但有一个问题,它显示所有答案,这不是我想要的。我想要它,以便根据我单击的按钮,它只显示该按钮的答案,而不会显示其他答案。


如何操作代码来合并我猜测的某种软循环来检索已单击的按钮并显示相应的答案并隐藏所有其他答案?


var answers = document.getElementsByClassName("showme");

  var revealAnswers = document.getElementsByClassName("answerbtn");


function revealAnswersFunction() {


  if (answers.style.display === "none") {

    answers.style.display = "inline-block";

  } else {

    answers.style.display = "none";

  }

}


qq_遁去的一_1
浏览 108回答 3
3回答

慕虎7371278

function revealAnswersFunction(e) {var all =document.getElementsByClassName('showme');for(let i=0;i<all.length;i++){&nbsp; &nbsp;all[i].style.display='none';&nbsp; &nbsp;}var div = e.nextSibling.nextElementSibling;div.style.display = "inline-block";&nbsp;&nbsp;}<div class="showhim">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <button class="answerbtn" onclick="revealAnswersFunction(this)">Click/Tap To Reveal Answers:</button>&nbsp; &nbsp; <div class="showme" style="display:none">1D, 2C</div></div>&nbsp; <div class="showhim">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <button class="answerbtn" onclick="revealAnswersFunction(this)">Click/Tap To Reveal Answers:</button>&nbsp; &nbsp; <div class="showme" style="display:none">1A, 2D</div></div>&nbsp; <div class="showhim">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <button class="answerbtn" onclick="revealAnswersFunction(this)">Click/Tap To Reveal Answers:</button>&nbsp; &nbsp; <div class="showme" style="display:none">1B, 2B</div></div>

喵喵时光机

一个简单的解决方案是在函数中使用索引:<div class="showhim">         <button class="answerbtn" onclick="revealAnswersFunction(0)">Click/Tap To Reveal Answers:</button>    <div class="showme" style="display:none">1D, 2C</div></div>  <div class="showhim">         <button class="answerbtn" onclick="revealAnswersFunction(1)">Click/Tap To Reveal Answers:</button>    <div class="showme" style="display:none">1A, 2D</div></div>  <div class="showhim">         <button class="answerbtn" onclick="revealAnswersFunction(2)">Click/Tap To Reveal Answers:</button>    <div class="showme" style="display:none">1B, 2B</div></div>function revealAnswersFunction(index) {  if (answers[index].style.display === "none") {    answers[index].style.display = "inline-block";  } else {    answers[index].style.display = "none";  }}在 HTML 中,我向onclick="revealAnswersFunction(0)" . 然后在函数中使用该索引来获取要显示的答案。为了将来参考,当您调用它时document.getElementsByClassName(),它会返回一个数组,因此您将需要要修改的数组项的索引。一个简单的解决方案是在函数中使用索引:<div class="showhim">         <button class="answerbtn" onclick="revealAnswersFunction(0)">Click/Tap To Reveal Answers:</button>    <div class="showme" style="display:none">1D, 2C</div></div>  <div class="showhim">         <button class="answerbtn" onclick="revealAnswersFunction(1)">Click/Tap To Reveal Answers:</button>    <div class="showme" style="display:none">1A, 2D</div></div>  <div class="showhim">         <button class="answerbtn" onclick="revealAnswersFunction(2)">Click/Tap To Reveal Answers:</button>    <div class="showme" style="display:none">1B, 2B</div></div>function revealAnswersFunction(index) {  if (answers[index].style.display === "none") {    answers[index].style.display = "inline-block";  } else {    answers[index].style.display = "none";  }}在 HTML 中,我向onclick="revealAnswersFunction(0)" . 然后在函数中使用该索引来获取要显示的答案。为了将来参考,当您调用它时document.getElementsByClassName(),它会返回一个数组,因此您将需要要修改的数组项的索引。一个更简洁的解决方案是使用事件侦听器将事件添加onclick到 javascript 中的按钮。

德玛西亚99

首先使用hide()方法隐藏所有答案,然后更改当前答案的显示属性。function revealAnswersFunction(e) {&nbsp; $('.showme').hide();&nbsp; e.target.nextElementSibling.style.display = 'inline-block';}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class="showhim">&nbsp;&nbsp; &nbsp; <button class="answerbtn" onclick="revealAnswersFunction(event)">Click/Tap To Reveal Answers:</button>&nbsp; &nbsp; &nbsp;<div class="showme" style="display:none">1D, 2C</div></div>&nbsp; &nbsp;&nbsp;<div class="showhim">&nbsp;&nbsp; &nbsp; <button class="answerbtn" onclick="revealAnswersFunction(event)">Click/Tap To Reveal Answers:</button>&nbsp; &nbsp; <div class="showme" style="display:none">1A, 2D</div></div>&nbsp; &nbsp;&nbsp;&nbsp;<div class="showhim">&nbsp;&nbsp; &nbsp; <button class="answerbtn" onclick="revealAnswersFunction(event)">Click/Tap To Reveal Answers:</button>&nbsp; &nbsp; &nbsp;<div class="showme" style="display:none">1B, 2B</div>&nbsp;</div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5