当我点击任何按钮时,隐藏或取消隐藏的唯一文本是第一个答案。 我希望按钮能够处理本地答案。就像问题旁边的按钮应该只对答案一起操作,而答案2旁边的按钮应该只隐藏或取消隐藏问题2.我宁愿不为每个问题使用get by id。
String YourNumberString = String.format("%d", YourNumberInteger);
答案 0 :(得分:1)
我希望它会对您有所帮助
function showFunction(val) {
var x = document.getElementsByClassName("answer"+val)[0].style.color = 'black'
}
function hideFunction(val) {
var x = document.getElementsByClassName("answer"+val)[0].style.color = 'white'
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Book Title</title>
<style>
</style>
</head>
<body>
<h3> Flashcards </h3>
<p class="question">
This is the First Question
</p>
<div class="answer1">
<p>
This is the First Answer
</p>
</div>
<div>
<label>Check Answer:</label>
<button onclick="showFunction(1)">Show Answer</button>
<button onclick="hideFunction(1)">Hide Answer</button>
</div>
<p class="question">
This is the Second Question
</p>
<p class="answer2" >
This is the Second Answer
</p>
<br />
<div>
<label>Check Answer:</label>
<button onclick="showFunction(2)">Show Answer</button>
<button onclick="hideFunction(2)">Hide Answer</button>
</div>
<p class="question">
This is the Thrird Question
</p>
<p class="answer3">
This is the Third Answer
</p>
<br />
<div>
<label>Check Answer:</label>
<button onclick="showFunction(3)">Show Answer</button>
<button onclick="hideFunction(3)">Hide Answer</button>
</div>
</body>
</html>
答案 1 :(得分:0)
如果您试图隐藏内容,请在每个div上使用隐藏
<div id="string" hidden>
如果你想在按下按钮时隐藏整个div,我会推荐这行编码
var x = document.getElementById("Section_1");
x.style.display = 'none';
var x = document.getElementById("Section_2")
x.style.display = 'block'
如果你有正确的id,'none'会隐藏一个部分 和块将根据需要显示一个部分。
如果您只是更改字体颜色,那么
<font color="string">