Редактировать после комментария от OP DEMO
checkForDiv1 = function(){ // function to check the requirement and do things accordingly
if($('.div.expand').length == 0){ // check for expand class div
$('.div1').addClass('expand')
}else{
$('.div1').removeClass('expand')
}
};
$('.div').click(function(){
$(this).toggleClass('expand');
checkForDiv1(); // check the requirement and do the required
});
checkForDiv1(); // call it to make it initially expanded
Если вы хотите перейти с HTML и не изменяя его, это может помочь
DEMO
$('div').click(function(){
$('div').not($(this)).removeClass('expand'); // you remove class expand from all except the one which is clicked
$(this).addClass('expand') // add expand class to the one which is clicked
});
CSS
div {
height:30px; // initial height required for transitions
transition: all 1s ease // some smooth transitions
}
.expand {
height: 200px;
background:red;
}
https://jqueryui.com/accordion/ –
@Sunit, Inthis URL, когда я пытаюсь для закрытия 2 или 3 или 4 div, он не рухнет. – Raja
PLZ проверить мой ответ ... ниже живой демо –