»
Tiếng AnhTiếng PhápTiếng Việt

In - Bảng hỏi đáp - JavaScriptBank.com

Phiên bản đầy đủ: jsB@nk » Trình đơn » Đồng hồ » Bảng hỏi đáp
URL: https://www.javascriptbank.com/expanding-questionanswer-script.html

Bảng hỏi đáp © JavaScriptBank.comHiệu ứng tạo một bảng hỏi đáp trên trang web có thể xem dễ dàng và tiện lợi.

Phiên bản đầy đủ: jsB@nk » Trình đơn » Đồng hồ » Bảng hỏi đáp
URL: https://www.javascriptbank.com/expanding-questionanswer-script.html



CSS
<style type="text/css">a, a:visited, a:active {font-size:10pt;font-weight:normal;font-family:sans-serif;color:black;text-decoration:underline;}a:hover {font-size:10pt;font-weight:normal;font-family:sans-serif;color:red;text-decoration:none;}body{font-size:10pt;font-weight:normal;font-family:sans-serif;color:black;background-color:rgb(255,255,255);}pre, .pre {font-size:10pt;font-weight:normal;font-style:arial;color:black;}.question {cursor:default;display:block;font-size:12pt;font-family:serif;color:black;font-weight:bold;}.answer {display:none;color:#00326d;font-size:10pt;font-family:sans-serif;padding: 2 0 5 0;}.container{display:block;}</style><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


JavaScript
<script language="javascript" type="text/javascript" title="qa.js">var ids=new Array();function QAinit(){if(document.getElementById){var tids=document.getElementsByTagName('div');for(i=0;i<tids.length;i++)if(tids[i].className=="question")ids[ids.length]=tids[i];for(i=0;i<ids.length;i++)ids[i].onmouseup=setstate;}}function setstate(){for(i=0;i<ids.length;i++){if(ids[i]==this){var d=this.parentNode.getElementsByTagName('div')[1];if(d.style.display=="block")d.style.display="none";else d.style.display="block";}}}function expandall(){if(document.getElementById){for(i=0;i<ids.length;i++)ids[i].parentNode.getElementsByTagName('div')[1].style.display="block";}}function collapseall(){if(document.getElementById){for(i=0;i<ids.length;i++)ids[i].parentNode.getElementsByTagName('div')[1].style.display="none";}}window.onload=QAinit;</script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<a href="javascript:expandall()">Expand all</a> | <a href="javascript:collapseall()">Collapse all</a><br><br>  <div class="container"><div class="question"><b>Q:</b> What does this script do?</div>  <div class="answer">  <b>A:</b> It provides a collapsable/expandable interface. The only answers the user sees are the ones they want. It also keep your page clean-looking and helps minimize scrolling for long lists.  </div></div>  <div class="container"><div class="question"><b>Q:</b> How do I use it?</div>  <div class="answer">  <b>A:</b> Just click on the question once and the answer will expand. Click on the question again and the answer will collapse. There is a provision in the script to expand or collapse all the answers at once, which is explained later on below.  </div></div>  <div class="container"><div class="question"><b>Q:</b> How do I install it?</div>  <div class="answer">  <b>A:</b> Basically, follow these steps:<br>  <ol>  <li>Paste a few lines of style-sheet code into the HEAD section of your page:<pre>  &lt;style type=&quot;text/css&quot;&gt;  .question { cursor:default; display:block; width:500px; }  .answer { display:none; width:500px; padding: 2 0 5 0; }  .container{ display:block; }  &lt;/style&gt;</pre>  You can add other style attributes such as font in there too. You can change the settings in the styles above, but these give the best look.<br><br>  <li>Copy the file &quot;qa.js&quot; to your server and add the following line to the HEAD section of your page:<pre>  &lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot; src=&quot;qa.js&quot;&gt;&lt;/script&gt;</pre>  <li>Every question/answer pair needs 3 seperate &lt;DIV&gt; elements; the question container, the answer container, and another container that &quot;holds&quot; both of them. Basically, the structure is like this:<pre>    &lt;div class=&quot;container&quot;&gt;  &lt;div class=&quot;question&quot;&gt; Your question goes here... &lt;/div&gt;  &lt;div class=&quot;answer&quot;&gt; Your answer goes here... &lt;/div&gt;  &lt;/div&gt;</pre>  The class names are the ones in the style-sheet code in step 1.<br><br>  </ol>  You can &quot;nest&quot; these elements so you can have question/answer pairs that only appear when a certain answer is shown, but it gets a little tough to explain. Basically, (if you&#39;re brave) just create another 3 element set inside the answer element. You can multi-nest as much as you like, the script will still know which one to expand. Just don&#39;t come crying to me if your page&#39;s code gets confusing. :)  </div></div>  <div class="container"><div class="question"><b>Q:</b> Can I expand all the questions at once?</div>  <div class="answer">  <b>A:</b> Yes! There are provisions to expand and collapse all of them at once. There are two javascript functions: <span class="pre">expandall()</span> and <span class="pre">collapseall()</span>. You can use them like any other link, see the example below:<pre>  &lt;a href=&quot;javascript:expandall()&quot;&gt;Expand All&lt;/a&gt;  &lt;a href=&quot;javascript:collapseall()&quot;&gt;Collapse All&lt;/a&gt;</pre>  This is how the links near the top of the page work.  </div></div>  <div class="container"><div class="question"><b>Q:</b> What browsers does this script work in?</div>  <div class="answer">  <b>A:</b> This script ONLY works in &quot;standards&quot; compliant browsers such as Netscape 6+ and Internet Explorer 5+. It should not cause errors in older browsers, it just will not function.  </div></div>  <div class="container"><div class="question"><b>Q:</b> Anything else?</div>  <div class="answer">  <b>A:</b> Yes, there are a few things to consider:  <ol>  <li>Do not use the class names explained earlier anywhere else in your page or you&#39;ll get some weird effects or errors. This is because the script hunts down all DIV elements with a classname of &quot;question&quot;, it then sets mouse events to expand/collapse its sister DIV (class name &quot;answer&quot;). So, if the format explained earlier isn&#39;t followed, the script will generate errors.<br><br>  <li>This script uses the &quot;window.onload&quot; event handler. If you have another script on your page that sets this event, then only one will work. You can find more information on how to overcome this issue at my <a href="http://scriptasylum.com/faqpage.html" target="_blank">FAQ page</a>, just scroll down to the appropriate section.<br><br>  <li>You can have the answers shown by default by changing the value <span class="pre">none</span> to <span class="pre">block</span> in the <span class="pre">display:</span> portion of the <span class="pre">.answer</span> stylesheet declaration.  </div></div><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->