[英]Javascript Multiple menu. If i click on one ul i want the other to close. (toogle). It keeps being open, how to fix?
Hopefully you can help me out with my problem. 希望你能帮助我解决我的问题。 Been annoying be for quite some time now.
现在已经很烦人了。
Trying to make my menu work. 试图使我的菜单正常工作。
When i click on a block i want it to open, and close the other tab "if" another one is open. 当我单击一个块时,我希望它打开,然后关闭另一个选项卡“如果”,则打开另一个选项卡。
Best regards jfb 最好的问候jfb
HERE IS JSFIDDLE http://jsfiddle.net/3ZWZu/ INCLUDES HTML, CSS, JS(jQuery) 这里是JSFIDDLE http://jsfiddle.net/3ZWZu/ 包括HTML,CSS,JS(jQuery)
$(document).ready(function() {
$('.ac-menu .topLevel').click(function(e) {
e.preventDefault();
if($('.ac-menu .topLevel ul').hasClass('open') === true){
$('.ac-menu .topLevel ul').removeClass('open');
$('.ac-menu .topLevel ul').addClass('closed');
$('.ac-menu .topLevel ul').slideUp(300);
}
if($(this).next('ul').hasClass('closed') === true){
$(this).next('ul').removeClass('closed');
$(this).next('ul').slideDown(300);
$(this).next('ul').addClass('open');
}
});
});
Presuming you use JQuery, try this one: 假设您使用JQuery,请尝试以下一种方法:
$(document).ready(function() {
$('.ac-menu .topLevel').click(function(e) {
e.preventDefault();
$('.ac-menu ul.open').removeClass('open').addClass('closed').slideUp(300);
$(this).next('ul.closed').removeClass('closed').slideDown(300).addClass('open');
});
});
Basically the idea behind this is that with ul.open
or ul.closed
selector you will only select the ul
s which have that specific class set. 基本上,这背后的想法是,使用
ul.open
或ul.closed
选择器,您只会选择设置了该特定类的ul
。 If there are none - then JQuery will return an empty set and will not apply those operations to anything - that is the way JQuery works. 如果不存在,那么JQuery将返回一个空集,并且不会将这些操作应用于任何事物,这就是JQuery的工作方式。 Furthermore, it allows you to chain your commands, like shown in my example.
此外,它允许您链接命令,如我的示例所示。
I think you need that jsfiddle 我认为你需要那个jsfiddle
i add topLevel
class to li instead of a href
我将
topLevel
类添加到li而不是a href
$(document).ready(function() {
$('.ac-menu li.topLevel').click(function(e) {
e.preventDefault();
var isClosed = $(this).find('ul').hasClass('closed');
$('.ac-menu li.topLevel ul').removeClass('open').addClass('closed').slideUp(300);
if( isClosed){
$(this).find('ul').addClass('open').removeClass('closed').slideDown(300);
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.