Javascript jQuery的简单子菜单故障

Javascript jQuery的简单子菜单故障,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我的网站上有一个简单的菜单——当我点击一个主链接时,它会打开一个子菜单。但是,如果我单击另一个主链接,它不会关闭原始子菜单。我该如何解决这个问题 用一个例子来描述可能更容易-单击项目,然后单击更多项目: $('#菜单主菜单>li>a')。单击(函数(){ $(this.next().toggle(); //如果使用后有任何其他元素 //$(this.parent().find('ul').toggle(); //此外,如果你需要任何更好的效果,那么使用 //slideToggle()或fade

我的网站上有一个简单的菜单——当我点击一个主链接时,它会打开一个子菜单。但是,如果我单击另一个主链接,它不会关闭原始子菜单。我该如何解决这个问题

用一个例子来描述可能更容易-单击
项目
,然后单击
更多项目

$('#菜单主菜单>li>a')。单击(函数(){
$(this.next().toggle();
//如果使用后有任何其他元素
//$(this.parent().find('ul').toggle();
//此外,如果你需要任何更好的效果,那么使用
//slideToggle()或fadeToggle而不是toggle())
});
#菜单主菜单>li>ul{
显示:无
}
#菜单主菜单{
背景色:#eee;
宽度:200px;
}
#菜单主菜单{
位置:相对位置;
}
#菜单主菜单{
宽度:200px;
背景色:红色;
位置:绝对位置;
边际上限:0;
排名:0;
左:250px;
}


打开新子菜单时,应隐藏其他菜单项。您可以通过在事件处理程序中添加以下行来使用它

$(this) // The element that is clicked i.e. a
    .parent() // The direct parent of the clicked element i.e. li
    .siblings() // All the sibling elements
    .find('ul').hide(); // Find will search for the `ul` inside the siblings and hide it
演示:

$('#菜单主菜单>li>a')。单击(函数(){
$(this.parent().sides().find('ul').hide();
$(this.next().toggle();
});
#菜单主菜单>li>ul{
显示:无
}
#菜单主菜单{
背景色:#eee;
宽度:200px;
}
#菜单主菜单{
位置:相对位置;
}
#菜单主菜单{
宽度:200px;
背景色:红色;
位置:绝对位置;
边际上限:0;
排名:0;
左:250px;
}


打开新子菜单时,应隐藏其他菜单项。您可以通过在事件处理程序中添加以下行来使用它

$(this) // The element that is clicked i.e. a
    .parent() // The direct parent of the clicked element i.e. li
    .siblings() // All the sibling elements
    .find('ul').hide(); // Find will search for the `ul` inside the siblings and hide it
演示:

$('#菜单主菜单>li>a')。单击(函数(){
$(this.parent().sides().find('ul').hide();
$(this.next().toggle();
});
#菜单主菜单>li>ul{
显示:无
}
#菜单主菜单{
背景色:#eee;
宽度:200px;
}
#菜单主菜单{
位置:相对位置;
}
#菜单主菜单{
宽度:200px;
背景色:红色;
位置:绝对位置;
边际上限:0;
排名:0;
左:250px;
}

或:

或:


Put
$(“#menu primary menu>li>ul”).hide()$(this.next().toggle()之前编码>
id
s必须是唯一的..放入
$('#menu primary menu>li>ul')。隐藏()$(this.next().toggle()之前编码>
id
s必须是唯一的。你没有注意到重复的
id
s?@GuruprasadRao这很糟糕,但它不是问题的原因。是的,但当你回答问题时,如果你纠正并让OP纠正它,那么他就不会提出只是纠正类型的问题了。。右??)@Alejalapeno@GuruprasadRao我真的没注意到,谢谢你的指点。我只是专注于解决方案
$('#menu-primary-menu>li>a').click(function() {
      $(this).parents("ul").find("li>ul").not($(this).next()).hide();
      $(this).next().toggle();
    });