別の解決策は次のとおりです。
/**
* Make an accordion active
* @param {String} id ID of the accordion
*/
var activateAccordion = function (id) {
// Get the parents
var parents = $('a[href="#' + id + '"]').parents('.panel-group').children('.panel');
// Go through each of the parents
$.each(parents, function (idx, obj) {
// Check if the child exists
var find = $(obj).find('a[href="#' + id + '"]'),
children = $(obj).children('.panel-collapse');
if (find.length > 0) {
// Show the selected child
children.removeClass('collapse');
children.addClass('in');
} else {
// Hide the others
children.removeClass('in');
children.addClass('collapse');
}
});
};
コードの重要な部分は、.inを使用するだけでなく、 .collapseクラスを覚えている組み合わせです。
// Show the selected child
children.removeClass('collapse');
children.addClass('in');
と
// Hide the others
children.removeClass('in');
children.addClass('collapse');
上記の例は、Twitter の Bootstrap v3.3.4 でテストされています。