简体   繁体   English

如何在点击另一个时折叠打开的常见问题解答?

[英]How do I collapse the open FAQ when clicking on another?

I currently have the following code to make my FAQ buttons appear with the answer hidden until it is clicked. 我目前有以下代码,使我的常见问题解答按钮显示,其答案隐藏,直到它被点击。 On click it shows the answer, but it leaves the answer expanded for each question when you click on another which eventually becomes way too long for the page as well as just being too much text on the page. 单击它会显示答案,但是当您单击另一个问题时,它会为每个问题保留答案,这最终会变得对页面来说太长,而且页面上的文本太多。

<script type="text/javascript">
function toggleMe(a){

var e=document.getElementById(a);
if(!e)return true;
if(e.style.display=="none"){
e.style.display="block"
}
else{
e.style.display="none"
}
return true;
}
</script>

<input type="button"  onclick="return toggleMe('para1');" value="Question 1" class="buttonClass"><br>
<div class="button" id="para1" style="display:none" >

<br>Stupid, long answer
</div>
<br>
<input type="button" onclick="return toggleMe('para2')" value="Question 2" class="buttonClass"><br>
<div class="button" id="para2" style="display:none" >
<br>
Stupid, long answer 2
</div>
<br>

What I would like to happen is that when question1 is expanded and you click on another question, it not only expands that question, but would collapse question1. 我想要发生的是,当问题1被扩展并且你点击另一个问题时,它不仅扩展了这个问题,而且会崩溃问题1。

TIA TIA

Since all the answers have the class button ,you can hide the all divs with class button first and then run the rest of your code. 由于所有答案都有类button ,因此您可以先使用类button隐藏所有div,然后运行其余代码。

function toggleMe(a){
 //Collapse all answers
   var elements=document.getElementsByClassName("button");
   for (var i=0;i<elements.length;i+=1){
    elements[i].style.display = 'none';
   }
 //Toggle a particular answer
 var e=document.getElementById(a);
 if(!e) return true;
 if(e.style.display=="none"){
  e.style.display="block"
 }
 else{
  e.style.display="none"
 }
 return true;   
}

It iteratively sets the display:none property to all the divs with class button . 它迭代地将display:none属性设置为带有class button所有div。

I would rather than writing code for you like to give general idea. 我宁愿为你编写代码,也不愿意给出一般的想法。 This could be achieved by following steps: 这可以通过以下步骤来实现:

  1. Make all your answer divs marked with some class say answer. 让你所有的答案div标记为一些类说答案。
  2. Now, as all the divs are hidden for the first time. 现在,因为所有的div都是第一次隐藏。 Rather than display:none. 而不是显示:无。 Use a css class hide-answer to avoid repeating code. 使用css类hide-answer避免重复代码。

     .hide-answer{ display:none } 
  3. Now bind on answers div an onclick event. 现在绑定一个onclick事件的答案。 you could do this either by onclick element in div or use standard class name to manipulate dom and bind onclick event. 您可以通过div中的onclick元素或使用标准类名来操作dom并绑定onclick事件。 The onclick javascript call method is suppose toggleDivs() onclick javascript调用方法假设为toggleDivs()

  4. toggleDivs method should wisely set clicked questions class to show-answer which is like below: toggleDivs方法应该明智地将点击的问题类设置为show-answer,如下所示:

      .show-answer{ display:block; } 

And any other div with class show-answer to hide-answer . 任何其他div与类show-answer hide-answer

Reason for generalised steps it to let you try it out and learn more out of it. 广义步骤的原因让你尝试一下并从中学到更多。

Hope it helps! 希望能帮助到你!

I think you might want to add a function in your script like: 我想你可能想在你的脚本中添加一个函数,如:

function collapse_all(){
  var answers = document.getElementsByClassName("button");
  for (var j = 0; j < answers.length; j++) {
    answers[j].style.display="none";
  }
  return true;
}

And then a function that calls the two functions in sequence, collapsing everything before opening a new answer: 然后是一个按顺序调用两个函数的函数,在打开新答案之前折叠所有内容:

function open_answer(question){
    collapse_all();
    return toggleMe(question);
}

And then call that function from your HTML: 然后从HTML中调用该函数:

<input type="button"  onclick="return open_answer('para1');" value="Question 1" class="buttonClass">

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

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