jsBin demo
(请不要使用 “accordi 一个 N” ......这简直UFF nghhh :)
所以手风琴在演示中使用的ID accordion
like in
<div id="accordion">
只是不要使用ID! - 使用课程!
<div class="accordion"><!-- menu here bla bla --></div>
和比jQuery中:
/*jQuery time*/
$(document).ready(function(){
// $(".accordion h3").click(function(){ NO! use dynamic click delegation
$(document).on("click", ".accordion h3", function(){
//slide up all the link lists
// $("#accordian ul ul").slideUp(); Wrong. Reference to this!
$(this).closest(".accordion").find("ul ul").slideUp();
//slide down the link list below the h3 clicked - only if its closed
if(!$(this).next().is(":visible")) {
$(this).next().slideDown();
}
});
});
上面的代码也会工作为动态生成的手风琴原因,我们使用了.on()
方法,将委托点击事件到现有的,但也对未来元件。
你确定你的代码?不知道自己在做什么,很难说出自己做错了什么。 –