简体   繁体   English

单击父 div 时,子菜单隐藏/显示不锻炼

[英]Sub menu hide/show does not workout when click on the parent div

I have this vertical menu:我有这个垂直菜单:

<li class="vertical_menu_group_item orange" id="verticalMenu"><a class="mx-3" >Menu item</a>
    <ul style="display:none" class="submenus">
        <li><a href="">Sub menu item 1</a></li>
        <li><a href="">Sub menu item 2</a></li>
    </ul>
</li> 

And as you can see I have set the display:none for the class submenus and then within this Javascript code I tried to show the sub menu items, when user click on the menu item :如您所见,我已经为 class submenus设置了display:none ,然后在此 Javascript 代码中,当用户单击菜单项时,我尝试显示子菜单menu item

$(document).on('click','#verticalMenu',function(){
    let sbm = document.querySelector(".submenus");
    sbm.style.display = "block";
});

But now this thing does not work out and not shows the sub menu items.但是现在这个东西不起作用并且不显示子菜单项。

However, I have made sure that the onlick event runs successfully.但是,我已确保 onlick 事件成功运行。

I also tried showing the sub menu items like this:我还尝试显示这样的子菜单项:

sbm.toggle();

But it says: toggle is not a function但它说: toggle 不是 function

So what's going wrong here?那么这里出了什么问题? How can I properly hide/show this .submneus class when clicking on the link with id of verticalMenu ?单击带有verticalMenu id 的链接时,如何正确隐藏/显示此.submneus class ?

You are not removing the display block, therefore it will always be shown as soon as you clicked it once.您不会删除显示块,因此只要您单击一次,它就会始终显示。 You have to change the display to none again after your first click.第一次单击后,您必须再次将显示更改为无。

 $(document).on('click','#verticalMenu',function(){ let sbm = document.querySelector(".submenus"); console.log(sbm.style.display) if(sbm.style.display == "block") { sbm.style.display = "none"; } else { sbm.style.display = "block"; } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <li class="vertical_menu_group_item orange" id="verticalMenu"><a class="mx-3" >Menu item</a> <ul style="display:none" class="submenus"> <li><a href="">Sub menu item 1</a></li> <li><a href="">Sub menu item 2</a></li> </ul> </li>

A more elegant solution is the correct implementation of the toggle function from jquery.更优雅的解决方案是正确实现 jquery 中的切换 function。 I have also styled the cursor for your menu element so there is a visible response when the cursor is on it.我还为您的菜单元素设置了 cursor 的样式,因此当 cursor 位于其上时,会有可见的响应。

 $(document).on('click','#verticalMenu',function(){ $(".submenus").toggle("100"); //remove the 100ms if you don't want an animation. });
 .vertical_menu_group_item { cursor: pointer; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <li class="vertical_menu_group_item orange" id="verticalMenu"><a class="mx-3" >Menu item</a> <ul style="display:none" class="submenus"> <li><a href="">Sub menu item 1</a></li> <li><a href="">Sub menu item 2</a></li> </ul> </li>

See https://api.jquery.com/toggle/ for more infos.有关更多信息,请参阅https://api.jquery.com/toggle/

function Change(){
    let sbm = document.querySelector(".submenus");
     if (sbm.style.display !== "none") {  
                sbm.style.display = "none";  
            }  
            else {  
                sbm.style.display = "block";  
            }  
};

HTML:

<li class="vertical_menu_group_item orange" id="verticalMenu" onClick = Change()><a class="mx-3" >Menu item</a>
    <ul style="display:none" class="submenus">
        <li><a href="">Sub menu item 1</a></li>
        <li><a href="">Sub menu item 2</a></li>
    </ul>
</li> 

Here Try This Its Working在这里试试这个它的工作

The issue is you are trying to run the jQuery function with help of a javascript element object.问题是您正在尝试在 javascript 元素 ZA8CFDE6331BD59EB466AC96F8911 的帮助下运行 jQuery function。

You can use .toggle() like this:您可以像这样使用.toggle()

 $(document).on('click', '#verticalMenu', function() { $(".submenus").toggle(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <li class="vertical_menu_group_item orange" id="verticalMenu"> <a class="mx-3">Menu item</a> <ul style="display:none" class="submenus"> <li><a href="">Sub menu item 1</a></li> <li><a href="">Sub menu item 2</a></li> </ul> </li>

When you use display:none under <ul> tag it completely removes the element in the dom.当您在<ul>标记下使用display:none时,它会完全删除 dom 中的元素。 So when You Inspect it on browser you can't see the code there as it is non existence.因此,当您在浏览器上检查它时,您看不到那里的代码,因为它不存在。 Because of that toggle() is not working as the element doesn't exist in DOM and not getting class.因为那个toggle()不起作用,因为DOM中不存在该元素并且没有得到class。 That's the reason why it says "toggle is not a function".这就是为什么它说“切换不是功能”的原因。 So instead of using display property, use visibility instead.因此,不要使用display属性,而是使用visibility

<li class="vertical_menu_group_item orange" id="verticalMenu"><a class="mx-3" >Menu item</a>
<ul style="visibility:hidden;height:0px" class="submenus">
    <li><a href="">Sub menu item 1</a></li>
    <li><a href="">Sub menu item 2</a></li>
</ul>

Also set the height to 0px to remove space when using visibility:"hidden" property and height:"auto" when you want to show it.在使用 visibility:"hidden" 属性和 height:"auto" 时,将高度设置为 0px 以删除空间。

$(document).on('click','#verticalMenu',function(){
let sbm = document.querySelector(".submenus");
sbm.style.visibility = "visible";
sbm.style.height = "auto";
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM