点击链接折叠已展开的手风琴

时间:2012-10-30 14:33:49

标签: jquery

我有一个手风琴,当页面加载时,所有的手风琴都会折叠,当你点击其中一个手风琴时它会展开。它当前仅在您单击已折叠的链接时折叠。

如何更改下面的jQuery,以便当您点击已经展开的手风琴时它会崩溃?

我知道这个问题已经有一些变化,但我不能用我知道的小Jquery得到答案!

<script type="text/javascript">
jQuery(function($) {
$(document).ready(function(){   
    //Set default open/close settings
    $('.acc_container_products').hide(); //Hide/close all containers
    // $('.acc_trigger_products:first').addClass('active').next().show(); //Add "active" class to first trigger, then show/open the immediate next container

    //On Click
    $('.acc_trigger_products').click(function(){
    if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
        $('.acc_trigger_products').removeClass('active').next().slideUp(); //Remove all .acc_trigger classes and slide up the immediate next container
        $(this).toggleClass('active').next().slideDown(); //Add .acc_trigger class to clicked trigger and slide down the immediate next container
    }
    return false; //Prevent the browser jump to the link anchor
    });
});
});
</script>

HTML:

                        <h2 class="acc_trigger_products"><a href="#">Technical specification</a></h2>
                    <div class="acc_container_products">
                        <div class="block_products">    
                            <div class="included-content">
                                <ul style="margin-top:0px!IMPORTANT;margin-bottom:0px!IMPORTANT;">
                                <?php while(the_repeater_field('technical_specification')): ?>
                                    <li>
                                        <?php echo the_sub_field('technical_specification'); ?>
                                        <?php if(get_sub_field('extra_information') != "") { ?>
                                            <img title="<?php echo the_sub_field('extra_information'); ?>" class="helpButton" src="<?php bloginfo('stylesheet_directory'); ?>/images/rogue-resolutions-help-button.png" height="20" width="20" />
                                        <?php } ?>
                                    </li>
                                <?php endwhile; ?>
                                </ul>
                            </div>
                        </div>      
                    </div>

CSS:

    h2.acc_trigger_products {
    color:black;
    font-family:Helvetica, Arial;
    font-size:14px;
    padding: 0;
    margin: 2px 0 0 0;
    background-image: url('images/rogue-resolutions-products-acc.png');
    background-repeat:no-repeat;
    height: 32px;
    line-height: 32px;
    width: 298px;
    font-weight: normal;
    float: left;
}
h2.acc_trigger_products a {
    color:black!IMPORTANT;
    font-family:Helvetica, Arial;
    font-size:14px;
    text-decoration: none;
    display: block;
    padding: 0 0 0 15px;
    font-weight:bolder; 
}
h2.acc_trigger_products a:hover {
    color:black!IMPORTANT;
    font-family:Helvetica, Arial;
    font-size:14px;
    font-weight:bolder;
}
h2.active {background-position: left bottom;}
.acc_container_products {
    margin: 0px; 
    padding: 0;
    overflow: hidden;
    font-size: 1.2em;
    width: 296px;
    clear: both;
    background-color: #ccc;
}
.block_products {
    padding:10px;
    width:276px;
    }

.acc_container_products p.title {
    margin:0px!IMPORTANT;
    }

.acc_container_products p {
    margin-top:0px!IMPORTANT;
    }

1 个答案:

答案 0 :(得分:2)

假设整个HTML重复块,请使用以下脚本:

$(document).ready(function () {
    $('.acc_container_products').hide(); //hide all at start
    $('.acc_trigger_products').click(function(){
        $('.acc_trigger_products').removeClass('current');  //remove the 'current' class
        $(this).addClass('current'); //add 'current' class to clicked item
        $('.acc_trigger_products:not(.current)').removeClass('active').next().slideUp(); //slide up items which are not the 'current' one

        $(this).toggleClass('active').next().slideToggle(); //toggle class and visibility
    });
});​

请参阅小提琴:http://jsfiddle.net/PgkE2/4/