如果手风琴打开或关闭,交换jQuery手风琴的标题

时间:2012-10-22 12:08:42

标签: javascript jquery html accordion

我95%完成了这个(我希望)。我正在研究一种手风琴功能,根据手风琴的打开或关闭,它会改变手风琴头部的文字。 (我有一个正负图像工作)。

目前,如果客户点击手风琴打开并关闭它,标题会正确更改,但如果客户打开不同的手风琴,原手风琴会向上滑动,但标题不会更新。我已经尝试了一些我认为会起作用的选项,但还没有成功。

我做了一个jsfiddle here

jquery是:

 $(document).ready(function() {
// accordion functionality 
$('.accordion-head').click(function(e){
    var content = $(this).parent().find('.accordion-content');
    var head = $(this).parent().find('.accordion-head');
    var $showMore = "Show me more";
    var $hideContent = "Hide this offer";            

    content.addClass('actual');

    $('.accordion-content').not('.actual').each(function(){
        if ($(this).hasClass('accordion-opened')){
            $(this).slideUp(200,function(){
                $(this).toggleClass('accordion-opened');
                $(this).parent().find('.accordion-head').toggleClass('accordion-open');
            });
        }
    });

    $(this).toggleClass('accordion-open');
    content.removeClass('actual');               

    if($(this).hasClass('accordion-open')){
        content.slideDown(200,function(){
            content.toggleClass('accordion-opened');
            head.html($hideContent);
        });
    }else{
        content.slideUp(200,function(){
            content.toggleClass('accordion-opened');
            head.html($showMore);
        });
    }
    e.preventDefault();
   });

 });​

HTML看起来像这样:

  <div class="accordion">
         <div class="accordion-head">
                        <a href="#">
                           <span class="accordion-heading">Show me more</span>
                        </a>
                    </div>
                    <div class="accordion-content">
                        Including Thames Water, Severn Trent, Anglian Water, Yorkshire Water and many more, plus all council tax local authorities. Cashback on mortgage payments up to maximum monthly mortgage payment of £1,000.
                    </div>
                </div>


                <span class="spacer"></span>

                <div class="accordion">
                    <div class="accordion-head">
                        <a href="#">
                            <span class="accordion-heading">Show me more</span>
                        </a>
                    </div>
                    <div class="accordion-content">
                        Including British Gas, SSE, EDF Energy, E.ON, npower and many more.
                    </div>
                </div>
                <span class="spacer"></span>
            etc.

希望有人可以提供帮助。

2 个答案:

答案 0 :(得分:2)

您可以通过删除head.html($showMore)&amp ;,崩溃循环期间重写标题。改变

$(this).parent().find('.accordion-head').toggleClass('accordion-open');

$(this).parent().find('.accordion-head').toggleClass('accordion-open').html($showMore);

答案 1 :(得分:1)

您还可以在点击功能中插入$('.accordion-head').html($showMore);,这会更新所有标题以显示更多标题,然后根据条件更新各个标题。
    var $ showMore =“让我更多”;
    var $ hideContent =“隐藏此优惠”;
    的 $( '手风琴式头。')的HTML($相册更多>>暂);
    content.addClass( '实际');