2013-06-30 64 views
0

所以我試圖讓我的手風琴菜單起作用,以便當你點擊一個菜單並打開時,然後點擊另一個菜單,上一個菜單應該關閉 - 但似乎會自動打開,然後點擊它時關閉菜單。jQuery未隱藏的手風琴兒童

有人可以檢查我的代碼,看看我失蹤了嗎?我嘗試使用兄弟功能,但它根本不起作用,查找功能導致上述問題。

HTML

設計系列

<div class="sideContent"> 
    <ul> 
     <li><a href="http://www.nerosdecoshoppe.com/collections/275070-assorted-designs">Assorted Designs</a></li> 
     <li><a href= "http://www.nerosdecoshoppe.com/collections/202951-kiss-me-goodnight">Kiss Me Goodnight</a></li> 
     <li><a href= "http://www.nerosdecoshoppe.com/collections/202954-natures-phone-call">Into the Wild</a></li> 
     <li><a href= "http://www.nerosdecoshoppe.com/collections/202949-alice-n-stripes">Alice n Stripes</a></li> 
     <li><a href= "http://www.nerosdecoshoppe.com/collections/202950-magical-kingdom">Magical Kingdom</a></li> 
    </ul> 
    </div> 
</div> 

<div> 
    <a href="%20#" class="sideHeader">decoden cases</a> 

    <div class="sideContent"> 
    <ul> 
     <li><a href="http://www.nerosdecoshoppe.com/collections/202952-whip-cream">WhipCream</a></li> 
     <li><a href="http://www.nerosdecoshoppe.com/collections/202953-rhinestone-pearls">Rhinestones/Pearls</a></li> 
    </ul> 
    </div> 
</div> 

的jQuery:

$(document).ready(function() { 
    $('.sideContent').hide(); 
    $('.sideHeader').click(function() { 
     event.preventDefault(); 
     $(this).next().slideToggle('fast') 
     //.parent().parent().find('.sideContent:visible').slideUp('fast'); 
     .parent().parent().siblings.find('.sideContent:visible').slideUp('fast'); 
    }); 
}); 

這裏有一個jFiddle link它表明我無動作

回答

0

你可以做這樣的事情

$(document).ready(function() { 
    $('.sideContent').hide(); 
    $('.sideHeader').click(function() { 
     event.preventDefault(); 
     $('.sideContent').slideUp('fast'); 
     $(this).next().slideToggle('fast'); 

    }); 
}); 

DEMO

編輯:新版本涵蓋@nevermind

$(document).ready(function() { 
    $('.sideContent').hide(); 
    $('.sideHeader').click(function() { 
     event.preventDefault(); 
     var $slideContent = $(this).next(); 
     var slideDown = $slideContent.is(":not(:visible)"); 
     $('.sideContent').slideUp('fast'); 

     if (slideDown) 
      $slideContent.slideDown('fast'); 
    }); 
}); 

DEMO

+0

這工作正常,但如果用戶在同一項上點擊兩次 - 子菜單不能關閉... – sinisake

+0

@nevermind:夠公平的。添加了一個覆蓋該場景的答案。 –