0

我的全部展开/全部折叠功能有一点问题。

这是我的 jsfiddel 链接:

http://jsfiddle.net/HqXMN/10/

假设展开 Name1 和 Name2,然后点击展开全部按钮,它所做的是展开所有内容,但折叠 Name1 和 Name2。

我是否在我的 javascrit 函数中指定 css id?

$(function () {
    $(document).on('click', '.expandcollapse', function(e) {
        $('.collapse').each(function(index) {
            $(this).collapse("toggle");
        });

        if ($(this).html() == "<i class=\"icon-white icon-plus-sign\"></i> Expand All") {
            $(this).html("<i class=\"icon-white icon-minus-sign\"></i> Collapse All");
        }
        else {
            $(this).html("<i class=\"icon-white icon-plus-sign\"></i> Expand All");
        } 
    });
});
4

1 回答 1

1

您需要根据按钮的当前状态执行特定操作。一种方法是在按钮处于给定状态时为其分配特定类。

注意:似乎由于存在组管理,当您使用展开/折叠所有按钮和单个控件的组合时,状态似乎变得混乱。

$(document).on('click', '.expandcollapse', function (e) {
    // Perform specific action based on current state
    if ($(this).hasClass('collapseAll')) {
        $('.collapse').collapse('hide');
        $(this).removeClass('collapseAll').html("<i class=\"icon-white icon-plus-sign\"></i> Expand All");
    } else {
        $('.collapse').collapse('show');
        $(this).addClass('collapseAll').html("<i class=\"icon-white icon-minus-sign\"></i> Collapse All");
    }
});

演示

要关闭组管理并查看其在该场景中如何正常工作,您可以通过删除data-parent='#programs-accordion'所有项目或将以下内容放在 Javascript 顶部来执行此操作:

$('.collapse').collapse({
    toggle: false
});

DEMO - 没有群组管理

于 2013-03-07T20:37:39.730 回答