在jquery手风琴中,如何确保当你点击一个标题时,另一个标题关闭?

时间:2017-05-01 18:13:01

标签: javascript jquery jquery-ui

我正在使用Jquery Accordion来显示和隐藏某些div。还有一个“全部显示”/“全部展开”按钮。一切正常,除了当我点击第1节的标题然后点击第2节的标题时,第1节仍然展开。当另一个打开同时保留全部显示并隐藏所有功能时,如何使其折叠?我在StackOverFlow,jquery论坛和小提琴上尝试了多个答案。他们每个人似乎都缺少一件事。有人可以帮帮我吗?

这是一个几乎接近我想要的小提琴:http://jsfiddle.net/apd8526c/

这就是它正在做的事情:

var headers = $('#accordion .accordion-header');
var contentAreas = $('#accordion .ui-accordion-content ').hide();
var expandLink = $('.accordion-expand-all');

// add the accordion functionality
headers.click(function() {
    var panel = $(this).next();
    var isOpen = panel.is(':visible');

    // open or close as necessary
    panel[isOpen? 'slideUp': 'slideDown']()
        // trigger the correct custom event
        .trigger(isOpen? 'hide': 'show');

    // stop the link from causing a pagescroll
    return false;
});

// hook up the expand/collapse all
expandLink.click(function(){
    var isAllOpen = $(this).data('isAllOpen');

    contentAreas[isAllOpen? 'hide': 'show']()
        .trigger(isAllOpen? 'hide': 'show');
});

// when panels open or close, check to see if they're all open
contentAreas.on({
    // whenever we open a panel, check to see if they're all open
    // if all open, swap the button to collapser
    show: function(){
        var isAllOpen = !contentAreas.is(':hidden');   
        if(isAllOpen){
            expandLink.text('Collapse All')
                .data('isAllOpen', true);
        }
    },
    // whenever we close a panel, check to see if they're all open
    // if not all open, swap the button to expander
    hide: function(){
        var isAllOpen = !contentAreas.is(':hidden');
        if(!isAllOpen){
            expandLink.text('Expand all')
            .data('isAllOpen', false);
        } 
    }
});

1 个答案:

答案 0 :(得分:1)

检查此工作代码。我添加了

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <link href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" rel="stylesheet" />
    <script type="text/javascript">
        $(document).ready(function () {
            var headers = $('#accordion .accordion-header');
            var contentAreas = $('#accordion .ui-accordion-content ').hide();
            var expandLink = $('.accordion-expand-all');

            // add the accordion functionality
            headers.click(function () {
                 $('#accordion .ui-accordion-content:visible')['slideUp']().trigger('hide');
                var panel = $(this).next();
                var isOpen = panel.is(':visible');

                // open or close as necessary
                panel[isOpen ? 'slideUp' : 'slideDown']()
                    // trigger the correct custom event
                    .trigger(isOpen ? 'hide' : 'show');

                // stop the link from causing a pagescroll
                return false;
            });

            // hook up the expand/collapse all
            expandLink.click(function () {
                var isAllOpen = $(this).data('isAllOpen');

                contentAreas[isAllOpen ? 'hide' : 'show']()
                    .trigger(isAllOpen ? 'hide' : 'show');
            });

            // when panels open or close, check to see if they're all open
            contentAreas.on({
                // whenever we open a panel, check to see if they're all open
                // if all open, swap the button to collapser
                show: function () {
                    var isAllOpen = !contentAreas.is(':hidden');
                    if (isAllOpen) {
                        expandLink.text('Collapse All')
                            .data('isAllOpen', true);
                    }
                },
                // whenever we close a panel, check to see if they're all open
                // if not all open, swap the button to expander
                hide: function () {
                    var isAllOpen = !contentAreas.is(':hidden');
                    if (!isAllOpen) {
                        expandLink.text('Expand all')
                        .data('isAllOpen', false);
                    }
                }
            });
        });

    </script>
    <style>
        body {
            font-family: "Trebuchet MS", "Helvetica", "Arial", "Verdana", "sans-serif";
            font-size: 62.5%;
        }

        .accordion-expand-holder {
            text-align: center;
            padding: 10px;
        }
    </style>
</head>
<body>
    <p class="accordion-expand-holder">
        <a class="accordion-expand-all" href="#">Expand all</a>
    </p>
    <div id="accordion" class="ui-accordion ui-widget ui-helper-reset">

        <h3 class="accordion-header ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-corner-all">
            <span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-e"></span>
            Section 1
        </h3>
        <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom">
            <p>
                Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
            ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
            amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
            odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
            </p>
        </div>
        <h3 class="accordion-header ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-corner-all">
            <span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-e"></span>
            Section 2
        </h3>
        <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom">
            <p>
                Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
            ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
            amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
            odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
            </p>
        </div>
        <h3 class="accordion-header ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-corner-all">
            <span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-e"></span>
            Section 3
        </h3>
        <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom">
            <p>
                Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
            ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
            amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
            odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
            </p>
        </div>
    </div>
</body>
</html>