简体   繁体   English

如何使CSS下拉菜单即使在菜单外单击也能保持打开状态?

[英]how to get CSS drop down menus to stay open even when clicked outside the menu?

I have a request from a customer to handle their drop down menu system. 我有一个客户的要求来处理他们的下拉菜单系统。 I have the menus working as drop downs but they requested that the drop downs happen not on a hover, but must be "clicked" to display them, even all the crazy sub menus. 我将菜单用作下拉菜单,但他们要求下拉菜单不在悬停时发生,但必须“单击”以显示它们,甚至包括所有疯狂的子菜单。

Furthermore, they want the menus to stay open (pinned) in their revealed state even if the top level nav item is closed. 此外,即使顶层导航项目已关闭,他们也希望菜单在其显示状态下保持打开(固定)状态。 I know it's strange behaviour but it's what they want. 我知道这是奇怪的行为,但这就是他们想要的。 It should be noted that only one top nab menu item should be open at any time. 应当注意,任何时候都只能打开一个最上层的nab菜单项。 They are employing an iframe to display the hrefs so we don't need cookies to save the state of the menus at this time but it would be nice for the future ;) I'm not a javascript person, this was cobbled together from code I gathered but I'm at a loss. 他们正在使用iframe来显示href,因此我们现在不需要cookie来保存菜单的状态,但将来会很好;)我不是javascript人,这是从代码中整理来的我聚集了,但我很茫然。 I did manage to change a bit of JS at one time to handle opening the menus via a click but once the mouse moved out of the ULs it would disappear. 我确实设法一次更改了一些JS以通过单击来处理打开菜单的操作,但是一旦鼠标移出UL,它就会消失。

I've posted my code so far here: http://jsfiddle.net/9dJ9T/8/ 到目前为止,我已经在这里发布了我的代码: http : //jsfiddle.net/9dJ9T/8/

Can anyone get the menus to only display when clicked and stay opened and "pinned" until they are all subsequently "unpinned"? 任何人都无法使菜单仅在单击时显示,并保持打开和“固定”状态,直到随后全部“取消固定”为止。 Ideally I think jquery .toggle method would be better but this menu system has so many UL and LIs in it I can't imagine creating functions of each and every one!! 理想情况下,我认为jquery .toggle方法会更好,但此菜单系统中包含如此众多的UL和LI,我无法想象为每个菜单创建功能! Is there a way such a function could be abstracted to simplify the code and not end up with a huge list of functions? 有没有一种方法可以抽象出这样的函数以简化代码,而不会以大量的函数结尾? I'm eager to learn from the JS masters!! 我很想向JS大师学习!!

You don't need all that. 您不需要所有这些。 Remake it. 重新制作。 You can solve that in like 10 lines. 您可以用10行解决。

Add something like onclick="displaySubmenu('someIdOrSomethingOrWhateverYouWant');" 添加类似onclick =“ displaySubmenu('someIdOrSomethingOrWhateverYouWant');”的内容。 on each tab. 在每个标签上。

The js must be something like js一定是这样的

    function displaySubmenu(el)
    {
         // Hide all elements. (Easy to do)
         // Display the needed one. (document.getElementById('yourSetedId').style.display ='block');
    }

The rest is css and html. 其余的是css和html。

If you wanna keep the fade just keep that function and set it on your id. 如果您想保持淡入淡出,只需保留该功能并将其设置在您的ID上即可。

The easy part is just opening the right one and closing the others 简单的部分是打开正确的一个,然后关闭另一个

$(".dropTab").on("click", function(){
    $(".dropTab").hide();
    $(this).show();
})

the bit more complicated part is where the tab should close when the user clicks outside any menu area, like just on the main content. 更为复杂的部分是,当用户在任何菜单区域之外单击时(如在主要内容上),应关闭选项卡。

$("body").on("click",function(e) {
    if ($(e.target).closest(".headerBar").length) { 
        return;
    }

    $(".dropTab").hide();
});

try this 尝试这个

<div class="btn-group" id="myDropdown"><a class="btn dropdown-toggle" data-toggle="dropdown" href="#">Menu<span class="caret"></span></a><ul class="dropdown-menu"><li><a href="#">Choice1</a></li>
<li><a href="#">Choice2</a></li>
<li><a href="#">Choice3</a></li>
<li class="divider"></li>
<li><a href="#">Choice..</a></li></ul></div>

and the js 和js

$('#myDropdown').on('hide.bs.dropdown', function () {
return false;

}); });

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

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