简体   繁体   English

Javascript Multiple菜单。 如果我单击一个ul,我希望另一个关闭。 (切换)。 它一直保持打开状态,如何解决?

[英]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.openul.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.

相关问题 单击以打开另一个窗口时如何强制关闭弹出窗口? - How to force popup to close when I click to open an other one? 我想用一个按钮打开/关闭菜单,我只想使用 Javascript - I would like to open/close a menu with one button, I want to use only Javascript 我在jPushMenu中添加了一个“输入”,但是如果我在输入上“单击”,则菜单将关闭。 我可以防止这种行为吗? - I've added a “input” inside the jPushMenu, but if i “click” on the input the menu will close. I could I prevent this behaviour? jQuery .slideToggle导航菜单。 所有菜单都滑动,我只想要一个 - Jquery .slideToggle navigation menu. All menus slide and I only want one to 验证某些$ _POST变量是否正确后,我希望窗口关闭。 我该如何使用JQuery? - After I've validated if some $_POST variables are correct, I want my window to close. How do I do this with JQuery? 自定义边栏嵌套菜单。 我无法打开嵌套菜单 - Custom sidebar nested menu. I cannot open the nested menu 单击菜单时,使toogle选项卡打开,单击其他菜单项时,关闭 - Making toogle tabs open when clicked on menu, and close when other menu item is clicked jQuery下拉菜单。 页面标记无法正常工作-或我希望如何 - jQuery dropdown menu. Page marker not working correctly - or how i want it to 如何运行javascript打开/关闭菜单? - How can I run a javascript open/close menu? 我想给我添加一些动态菜单功能 <ul> 菜单。 - I'd like to add some dynamic menu functionality to my <ul> menu.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM