javascript - 如何使这个展开/折叠常见问题解答列表起作用?

标签 javascript jquery css html-lists accordion

这是我要展开/折叠的列表:我的目标只是切换展开/折叠,让这个列表像 Accordion 一样。我不明白的是如何使用下面提供的 javascript 将

从隐藏状态变为可见状态。非常感谢任何资源或直接帮助。

航运

    <li class="plusimageapply"><a name="faq-question">Why do I see prices for some items and not others? How do I get pricing on items that I want to buy?</a></li>
    
    <div style="display: none;">Ths is a sampel of an answer tot he above question.</div>
    
    <li class="plusimageapply"><a name="faq-question">How do I handle an overnight delivery?</a></li>
    
    <div style="display: none;">AMOeasy offers five overnight shipping options. During checkout, simply check the option that best meets your needs and process your order.
      <ul>
        <li>UPS orders must be placed before 5:30pm EST / 2:30pm PST.</li>
        <li>FedEx orders must be place before 8:00pm EST / 5:00pm PST.</li>
      </ul>
      If you are concerned that the item may not be in stock, please call customer service at 877-AMO-4LIFE (877-266-4543).
    </div>
    

以下是我正在使用的 JavaScript

<script type="text/javascript">
$(document).ready(function(){
    $('li a').click(function () {
        var questionname= this.name;
        $("#"+questionname).toggle();
        $(this).parent().toggleClass("minusimageapply");
    });
});
</script>

最佳答案

我可以建议一些有效的 HTML(假定 lionlyol 的有效子项或 uldiv 不是 either 的有效子级 这些元素),例如:

<ul>
    <li class="q">Question One</li>
    <li>first answer to question one</li>
    <li>second answer to question one</li>
    <li class="q">Question two</li>
    <li>first answer to question two</li>
    <li>second answer to question two</li>
    <li class="q">Question three</li>
    <li>first answer to question three</li>
    <li>second answer to question three</li>
</ul>

还有 jQuery:

$('li:not(".q")').hide();

$('li.q').click(
    function(){
        $('li:not(".q")').slideUp();
        $(this).toggleClass('open');
    });

JS Fiddle demo .

或者使用dl:

<dl>
    <dt>Question One</dt>
    <dd>first answer to question one</dd>
    <dd>second answer to question one</dd>
    <dt>Question two</dt>
    <dd>first answer to question two</dd>
    <dd>second answer to question two</dd>
    <dt>Question three</dt>
    <dd>first answer to question three</dd>
    <dd>second answer to question three</dd>
</dl>
​

还有 jQuery:

$('dd').hide();

$('dt').click(
    function() {
        var toggle = $(this).nextUntil('dt');
        toggle.slideToggle();
        $('dd').not(toggle).slideUp();
    });​

JS Fiddle demo .

关于javascript - 如何使这个展开/折叠常见问题解答列表起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10077683/

相关文章:

javascript - 无法在 Chrome 中为 "cloudfront.net"设置 cookie?

javascript - 单击div时如何使文本出现和消失?

css - 视网膜显示像素比和媒体查询

html - 如何使用 jstree 上下文菜单使框架集适当调整大小

javascript - 在 Facebook 上显示随机缩略图

javascript - 为什么 PayPal 无法识别我的 Items Array?

jquery - 底部 Phonegap 页面上的白色间隙

php - 使用一个输入提交多个变量

Javascript 代码在 IE8/7 中不起作用

javascript - 我怎样才能写出更干净的 JavaScript 代码?