jQuery Accordion definition list with multiple description items

送分小仙女□ 提交于 2019-12-10 05:31:50

问题


I can't seem to use jQuery Accordions with definition lists that have multiple desciption items (dd). The author's examples have only single dd items.

In the example below B2, B3 & C2 show onLoad, rather than hide like A1, B1 & C1 as I would prefer.

How would I achieve this?

jQuery('dl').accordion({ 
    event: 'click',     
    active: false, 
    animated: "bounceslide", 
    header: "dt" 
});​

<dl>

    <dt>A</dt>
    <dd>A1</dd>

    <dt>B</dt>
    <dd>B1</dd>
    <dd>B2</dd>
    <dd>B3</dd>

    <dt>C</dt>
    <dd>C1</dd>
    <dd>C2</dd>

</dl>

(Live jsFiddle version)


回答1:


  • demo: https://so.lucafilosofi.com/jquery-accordion-definition-list-with-multiple-description-items
$(function () {
    $('dt').on('click', function (e) {
        e.preventDefault();
        $(this).parent('dl').children('dd:visible').slideUp('slow');
        $(this).nextUntil('dt').filter(':not(:visible)').slideDown('slow');
    });
});

NOTE:

If you want multiple sections

open at once, don't use an accordion

  • An accordion doesn't allow more than one content panel to be open at the same time, and it takes a lot of effort to do that. If you are looking for a widget that allows more than one content panel to be open, don't use this. Usually it can be written with a few lines of jQuery instead, something like this: REFERENCE: http://jqueryui.com/demos/accordion/

hope this help! ;)




回答2:


The author claims the content needs to be adjacent to its header. Inspect Element shows that it ignores the extra <dd>'s:

<dt tabindex="0" aria-expanded="true" role="tab" class="ui-accordion-header ui-helper-reset ui-state-active ui-corner-top"><span class="ui-icon ui-icon-triangle-1-s"></span>B</dt>
<dd role="tabpanel" style="height: 20px; display: block; overflow: visible; padding-top: 0px; padding-bottom: 0px;" class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active">B1</dd>
<dd>B2</dd>
<dd>B3</dd>

If you have control over the HTML rendering, you can nest <p> elements inside the <dd>'s:

<dt>B</dt>
<dd>
    <p>B1</p>
    <p>B2</p>
    <p>B3</p>
</dd>



回答3:


Just to complement aSeptik's simple solution: If you want every item except the first one to be closed on page load, add this: $('dd:not(:first-of-type)').hide();

I would suggest using a class on Accordions, so you can still have unjqueryfized definition lists in your markup. Like this:

$('.accordion dd:not(:first-of-type)').hide();
$('.accordion dt').on('click', function (e) {
    e.preventDefault();
    $(this).parent('dl.accordion').children('dd:visible').slideUp('slow');
    $(this).nextUntil('dt').filter(':not(:visible)').slideDown('slow');
});

(and then using <dl class="accordion"><dt>... in your HTML)



来源:https://stackoverflow.com/questions/3404657/jquery-accordion-definition-list-with-multiple-description-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!