これをすべて機能させるための私の提案は次のとおりです...
HTML
<a class="iscriversi_toggle aShow">...iscriversi</a>
<br>
<a class="collaborare_toggle aShow" >...collaborare</a>
<br>
<a class="sostenerci_toggle aShow">...sostenerci</a>
<div id="iscriversi" class="divtoggle">div1</div>
<div id="collaborare" class="divtoggle">div2</div>
<div id="sostenerci" class="divtoggle">div3</div>
<a>
ここで、各要素にクラスを追加したことがわかります。クラスはaShow
です。これにより、同じ呼び出しで一度にすべてのアンカーにアクセスできます。
また、各要素divtoggle
にクラスを追加しました。div
同じ理由。
jQuery
jQuery(document).ready(function(){
// Hide all the divtoggle tags, and show the anchors
jQuery ('.divtoggle').hide();
jQuery(".aShow").show();
jQuery('.iscriversi_toggle').click(function(){
// Hide all the DIVs, and show just the one we want
jQuery ('.divtoggle').hide();
jQuery("#iscriversi").slideToggle();
});
jQuery('.collaborare_toggle').click(function(){
// Hide all the DIVs, and show just the one we want
jQuery ('.divtoggle').hide();
jQuery("#collaborare").slideToggle();
});
jQuery('.sostenerci_toggle').click(function(){
// Hide all the DIVs, and show just the one we want
jQuery ('.divtoggle').hide();
jQuery("#sostenerci").slideToggle();
});
});
コード内にコメントがあるので、簡単にフォローできます。これは、最初に持っていたものよりも少しクリーンで、よりスケーラブルです。また、ない側として、何度も指定する必要はありませんjQuery(document).ready(function(){
。一度だけ使用してください。
最後に、jsFiddle のデモを次に示します: DEMO