我有一组菜单,当用户点击选项栏(类似于移动/标记电子邮件的Gmail中的栏)时,我想要打开/关闭这些菜单。在jQuery中切换多个下拉菜单
当用户点击一个按钮时,出现菜单。当用户点击另一个按钮时,前一个菜单消失并出现新的菜单。这与以下代码一起工作。然而,不起作用的是,如果用户点击一个按钮,然后点击相同的按钮,菜单不会消失。
我明白为什么它不起作用,但不知道如何解决它。我认为我需要添加“如果打开的菜单是您点击的那个,关闭它并且不打开其他任何东西。”但是我试过的if语句没有这样做。
$("#moving ul li").click(function() {
// If the button clicked has the "active" class (meaning the menu is already open)
if ($("#moving ul li.active") == this) {
// Close the menu (removing the visible class) and make the button no longer active
$(this).toggleClass("active")
$(this).children("ul").toggleClass("visible")
// Otherwise ...
} else {
// Find the open one and close it
$("#moving ul li.active").toggleClass("active")
$("#moving ul li ul.visible").toggleClass("visible")
// Open this menu
$(this).toggleClass("active")
$(this).children("ul").toggleClass("visible")
}
})
谢谢!菜鸟的错误,但这清除了很多:) – Brenden 2011-03-21 20:45:23