[英]How can i fix this jquery script for submenu?
This is the code and this is the result: you can see test the menu by clicking inside. 这是代码,这就是结果:您可以通过单击内部看到测试菜单。
$(document).ready(function () {
var $links = $('#menu-menu-1 .menu-item a').click(function () {
var submenu = $(this).next();
$subs.not(submenu).hide()
submenu.toggle(500);
$("#menu-2").slideToggle(300);
});
var $subs = $links.next(); });
The problem is that if I click on the menu it appears the submenu, but if I don't close the submenu that I have open and I open another voice in the menu it doesn't work properly. 问题是,如果我单击菜单,它会出现子菜单,但是如果我不关闭已打开的子菜单,而又打开了菜单中的另一个声音,它将无法正常工作。
If I click another .menu-item a
when #menu-2 is open what happens? 如果在#menu-2打开时单击另一个.menu-item a
,会发生什么? The script close and open another submenu correctly but my #menu-2
close only close. 脚本关闭并正确打开另一个子菜单,但我的#menu-2
关闭仅关闭。 And then if I close .menu-item a
so... #menu-2
open. 然后,如果我关闭.menu-item a
那么... #menu-2
打开。 How can I do to fix? 我该如何解决?
Try 尝试
$(document).ready(function () {
var $menu2 = $("#menu-2");
var $links = $('#menu-menu-1 .menu-item a').click(function () {
var submenu = $(this).next();
$subs.not(submenu).hide();
var isVisible = submenu.stop(true, true).is(':visible');
$menu2.stop(true, true);
if (isVisible) {
submenu.hide(500);
$menu2.slideUp(300);
} else {
$menu2.slideUp(300, function () {
$menu2.slideDown(300);
submenu.show(500);
});
}
});
var $subs = $links.next();
});
I think you could simplify what you're trying to do with something along these lines (untested): 我认为您可以按照以下思路简化您尝试做的事情(未经测试):
$(function () {
var menuItems = $('#menu-menu-1 > li');
var submenus = $('ul', menuItems)
menuItems.click(function () {
submenus.slideUp(); // Hide all menus to their default hidden state
var submenu = $('ul', $(this)); // Show the child menu for the clicked menu
submenu.toggle(500);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.