1

展開/折りたたみたいリストは次のとおりです。私の目標は、展開/折りたたみを切り替えて、このリストをアコーディオンのように機能させることです。私が理解していないのは、以下に示す JavaScript を使用して、非表示の状態から表示状態に <div> を取得する方法です。リソースや直接の助けをいただければ幸いです。

運送

    <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>
4

3 に答える 3

5

次のような有効な HTML をいくつか提案できます ( anがorのみliの有効な子であり、 aがこれらの要素のいずれの有効な子でもない場合):oluldiv

<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 フィドルのデモ

または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 フィドルのデモ

于 2012-04-09T18:28:03.263 に答える
2

これを試してください

$('li a').click(function () {
    var $this = $(this);
    $this.parent().next().toggleClass("minusimageapply").slideToggle();
});​

*実例*

于 2012-04-09T18:19:04.470 に答える
0

それともこれ...

$('li.faq-answer').hide();
$('li.faq-question').click(function () {
    $(this).next().slideToggle();
});
li {
        list-style:none;
}
li.faq-question {
	font-weight: 700;
}
li.faq-question:hover {
	cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
	<li class="faq-question">Question 1</li>
	<li class="faq-answer">Curabitur blandit tempus porttitor.</li>
	<li class="faq-question">Question 2</li>
	<li class="faq-answer">Maecenas faucibus mollis interdum. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</li>
	<li class="faq-question">Question 3</li>
	<li class="faq-answer">Aenean lacinia bibendum nulla sed consectetur. Donec id elit non mi porta gravida at eget metus. Nullam quis risus eget urna mollis ornare vel eu leo.</li>
	<li class="faq-question">Question 4</li>
	<li class="faq-answer">Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</li>
	<li class="faq-question">Question 5</li>
	<li class="faq-answer">Praesent commodo cursus magna, vel scelerisque nisl consectetur et.</li>
</ul>

于 2015-05-19T16:21:13.417 に答える