简体   繁体   中英

“more/less” toggle content

So I have a classic "more/less" paragraph here:

http://jsfiddle.net/dVFaV/38/

When I click on "More info" the link reveals some content and at the same time it switches to "Less info" but I only want this to happen to one link at a time, as I will have more paragraphs on my page. Right now when I click on the "More info" link, the other one will switch too, which is what I'm trying to avoid.

Any help appreciated!!!

If your HTML structure stay like this, you can simply use :

function togglePanel() {
    var $this = $(this);
    if ($this.hasClass('button_open')) 
      $this.parent().next().slideDown('slow');
    else 
      $this.parent().next().slideUp('slow');

    $this.hide().siblings().show();
}

$('.adv-toggle-buttons > a').click(togglePanel);

​http://jsfiddle.net/WFC7s/

EDIT : Or like Bondye said in his answer, with slideToggle :

function togglePanel() {
    var $this = $(this);
    $this.parent().next().slideToggle('slow');
    $this.hide().siblings().show();
}

$('.adv-toggle-buttons > a').click(togglePanel);

Why dont you use toggle() and slideToggle() ?

HTML

<p class="adv-toggle-buttons">
    <a href="#">[+] More info</a>
    <a href="#" style="display: none;">[-] Less info</a>
</p>
<div class="adv-unit-options" style="display: none;">Lorem ipsum</div>

<p class="adv-toggle-buttons">             
    <a href="#">[+] More info</a>
    <a href="#" style="display: none;">[-] Less info</a>
</p>
<div class="adv-unit-options" style="display: none;">Lorem ipsum</div>

jQuery

$('.adv-toggle-buttons a').each(function() { 
    $(this).on('click', function(){
        $(this).parent().next().slideToggle();
        $(this).parent().find('a').each(function(){
            $(this).toggle();

        });
    });   
});

Fiddle

http://jsfiddle.net/dVFaV/40/

With the above you would have to keep 2 containers for the content. One for the short "less" version and one for the longer "more" version. I like to keep my content as easy to update as possible and actually ran into a situation where I couldn't do anything but add JS to the content to adjust the dom. I ended up using the same content just striping it down to text before I cut it. This would keep you from cutting any HTML tag and messing the content up.

Your HTML would be something like:

<div class="entry-content">
    <div class="event-detail">
        <div class="bio">
            <p><h2>Lorem 1</h2> ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        </div>
    </div>

    <div class="event-detail">
        <div class="bio">
            <p><h2>Lorem 2</h2> ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        </div>
    </div>
    </div>

The JS would look for each bio and add a more/less button and would also truncate the copy to a set value that works for your layout. The JS should be:

$(document).ready(function(){
    var num_of_bios = $('.entry-content .bio').length;
    for (i = 0; i < num_of_bios; i++) {
        bio_more = $('.bio:eq('+i+')').html();
        bio_less = $('.bio:eq('+i+')').text();
        bio_less = bio_less.substring(0,200);
        $('.bio:eq('+i+')').html(bio_less+' ... ');
        $('.bio:eq('+i+')').addClass('bio_less bio_'+i);
        $('.bio:eq('+i+')').parent().append('<div class="bio_more bio_'+i+'">'+bio_more+'</div>');
        $('.bio_more').hide();
        $('.bio:eq('+i+')').parent().append('<div class="bio_more_less" name="bio_'+i+'">more</div>');
    }
    $('.bio_more_less').click(function(){
        show_me = $(this).text();
        bio_name = $(this).attr('name');
        if( show_me === 'more'){
            $('.bio_less.'+bio_name).hide();
            $('.bio_more.'+bio_name).show();
            $(this).text('less');
        }
        else if( show_me === 'less'){
            $('.bio_less.'+bio_name).show();
            $('.bio_more.'+bio_name).hide();
            $(this).text('more');
        }
    });

    });

Here is a fiddle. http://jsfiddle.net/jaredlyvers/pezw77bk/10/

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