简体   繁体   中英

Accordion opening all divs on toggle

When clicking on my toggle, all my divs expand because the code is targeting the class for all the divs. I don't understand jQuery well enough to change it to only target the relevant div.

Javascript

$(function () {
    // The height of the content block when it's not expanded
    var postadjustheight = 340;
    // The "more" link text
    var postmoreText = "Click to Expand Post";
    // The "less" link text
    var postlessText = "Click to Condense Post";

    // Sets the .more-block div to the specified height and hides any content that overflows
    $("body.page-template-page_blog-php #content .post .entry-content").css('height', postadjustheight).css('overflow', 'hidden');

    // The section added to the bottom of the "more-less" div
    $("body.page-template-page_blog-php .entry-content").prepend('<a href="#" class="adjust"></a>');

    $("body.page-template-page_blog-php #content .post .adjust").toggle(function () {
        $("body.page-template-page_blog-php #content .post .entry-content").css('height', 'auto').css('overflow', 'visible');
        $(this).text(postlessText);
    }, function () {
        $("body.page-template-page_blog-php #content .post .entry-content").css('height', postadjustheight).css('overflow', 'hidden');
        $(this).text(postmoreText);
    });
});

HTML

    <div id="content">
    <div class="post">
        <div class="entry-content"><a class="adjust" href="#">Click to Expand Post</a>
            <p>Post Contents</p>
        </div>
    </div>
    </div>

Not knowing jQuery is the first step to learning jQuery.

Include this library http://plugins.learningjquery.com/expander/ and you should be able to expand/contract the specific classes you want depending on the toggle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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