在我的网站上,我有一个包含常见问题解答的页面。有很多问题,我想隐藏,直到有人点击问题,然后答案应该出现。我已经有了一个解决方案,我只是觉得这个解决方案太复杂了。我只是找不到解决方案如何使它更容易。你有什么建议吗?请参阅下面的代码,我希望它是不言自明的 - 如果不只是让我知道;-)
<script>
$(document).ready(function() {
for (var i = 1; i < 11; i++) {
answer = document.getElementById("answer" + i);
$(answer).hide();
}
question1 = document.getElementById("question1");
question2 = document.getElementById("question2");
question3 = document.getElementById("question3");
question4 = document.getElementById("question4");
question5 = document.getElementById("question5");
answer1 = document.getElementById("answer1");
answer2 = document.getElementById("answer2");
answer3 = document.getElementById("answer3");
answer4 = document.getElementById("answer4");
answer5 = document.getElementById("answer5");
$(question1).click(function() {
$(answer1).toggle();
});
$(question2).click(function() {
$(answer2).toggle();
});
$(question3).click(function() {
$(answer3).toggle();
});
$(question4).click(function() {
$(answer4).toggle();
});
$(question5).click(function() {
$(answer5).toggle();
});
});
</script>