Web サイトのツリー ナビゲーション メニューとして、コンテンツの非表示と表示 (トグル) に次の jQuery を使用しています。このコードは、クリックすると一度に 1 つの div しか表示されないため、非常に便利であることがわかりました。
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
function show_region(chosen_region) {
$('.city_div').each(function(index) {
if ($(this).attr("id") == chosen_region) {
$(this).slideDown(200);
}
else {
$(this).slideUp(600);
}
});
}
</script>
<style type="text/css">
.city_div {display: none;}
</style>
<a href="javascript:show_region('box01');">North</a><br>
<div class="city_div" id="box01">Div #01</div>
<a href="javascript:show_region('box02');">Centre</a><br>
<div class="city_div" id="box02">Div #02</div>
<a href="javascript:show_region('box03');">South</a><br>
<div class="city_div" id="box03">Div #03</div>
問題は、別の div を開くことによってのみ div を閉じることができることです。
2回目のクリックでdivを閉じる方法は?