Jquery accordion not removingClass and not swapping its Expand text for Collapse

十年热恋 提交于 2019-12-04 20:44:11

Here's my try:

jQuery.fn.initMenu = function() {
    var isCollapsed = true; // default value
    var collapseAll = 'Collapse All';
    var expandAll = 'Expand All';

    $('.swap').click(function() {
        if(!isCollapsed)
        {
            $('ul.menu li ul').slideToggle('normal');
            $('ul.menu li a').removeClass('active');
            $(this).text(expandAll);
            isCollapsed = true;
        } else {
            $('ul.menu li ul').slideToggle('normal');
            $('ul.menu li a').addClass('active');
            $(this).text(collapseAll);
            isCollapsed = false;
        }
        return false;
    });

    return this.each(function(){
        var theMenu = $(this).get(0);
        $('.acitem', this).hide();
        $('li.expand > .acitem', this).show();
        $('li.expand > .acitem', this).prev().addClass('active');
        $('li a', this).click(
        function(e) {
            e.stopImmediatePropagation();
            var theElement = $(this).next();
            var parent = this.parentNode.parentNode;
            if($(parent).hasClass('noaccordion')) {
                if(theElement[0] === undefined) {
                    window.location.href = this.href;
                }
                $(theElement).slideToggle('normal', function() {
                    if ($(this).is(':visible')) {
                        $(this).addClass('active');
                    }
                    else {
                        $(this).prev().removeClass('active');
                    }    
                });
                return false;
            }
            else {
                if(theElement.hasClass('acitem') && theElement.is(':visible')) {
                    if($(parent).hasClass('collapsible')) {
                        $('.acitem:visible', parent).first().slideUp('normal',
                        function() {
                            $(this).prev().removeClass('active');
                        }
                    );
                        return false;  
                    }
                    return false;
                }
                if(theElement.hasClass('acitem') && !theElement.is(':visible')) {         
                    $('.acitem:visible', parent).first().slideUp('normal', function() {
                        $(this).prev().removeClass('active');
                    });
                    theElement.slideDown('normal', function() {
                        $(this).prev().addClass('active');
                    });
                    return false;
                }


            }
        }
    );

    });
};

$(document).ready(function() {$('.menu').initMenu();});

Example link.
Since the .swap link is common for all, no need to have it inside the each loop.

You're pretty close, the problem exists in where you're using .text() to set the link.

$(this).text($(this).text().prev() == 'Click to Collapse');

That line of code attempts to set the text on $(this) with the return of $(this).text().prev() == 'Click to Collapse');

$(this).text() returns the string "Click to Collapse".

.prev() is not a valid function on strings, thus will result in a javascript error.

What you want to do is $(this).text("Click to Expand");

The end result will be something like this:

$('.swap').click(function()
            {
                if($(this).text() == 'Click to Collapse')
                {
                    $('ul.menu li ul').slideToggle('normal');
                $('ul.menu li a').removeClass('active');
                    $(this).text('Click to Expand');
                }else{
                    $('ul.menu li ul').slideToggle('normal');
                $('ul.menu li a').addClass('active');
                    $(this).text('Click to Collapse');
                    }
            }
    );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!