3 回答
TA贡献1802条经验 获得超4个赞
function revealAnswersFunction(e) {
var all =document.getElementsByClassName('showme');
for(let i=0;i<all.length;i++){
all[i].style.display='none';
}
var div = e.nextSibling.nextElementSibling;
div.style.display = "inline-block";
}
<div class="showhim">
<button class="answerbtn" onclick="revealAnswersFunction(this)">Click/Tap To Reveal Answers:</button>
<div class="showme" style="display:none">1D, 2C</div>
</div>
<div class="showhim">
<button class="answerbtn" onclick="revealAnswersFunction(this)">Click/Tap To Reveal Answers:</button>
<div class="showme" style="display:none">1A, 2D</div>
</div>
<div class="showhim">
<button class="answerbtn" onclick="revealAnswersFunction(this)">Click/Tap To Reveal Answers:</button>
<div class="showme" style="display:none">1B, 2B</div>
</div>
TA贡献1846条经验 获得超7个赞
一个简单的解决方案是在函数中使用索引:
<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 中的按钮。
TA贡献1770条经验 获得超3个赞
首先使用hide()方法隐藏所有答案,然后更改当前答案的显示属性。
function revealAnswersFunction(e) {
$('.showme').hide();
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">
<button class="answerbtn" onclick="revealAnswersFunction(event)">Click/Tap To Reveal Answers:</button>
<div class="showme" style="display:none">1D, 2C</div>
</div>
<div class="showhim">
<button class="answerbtn" onclick="revealAnswersFunction(event)">Click/Tap To Reveal Answers:</button>
<div class="showme" style="display:none">1A, 2D</div>
</div>
<div class="showhim">
<button class="answerbtn" onclick="revealAnswersFunction(event)">Click/Tap To Reveal Answers:</button>
<div class="showme" style="display:none">1B, 2B</div>
</div>
- 3 回答
- 0 关注
- 107 浏览
添加回答
举报