简体   繁体   English

如何从多导航栏打开下拉子菜单?

[英]How to open dropdown sub-menu from multi-navigation bar?

I am having trouble opening my dropdown sub-menu with CSS.我无法使用 CSS 打开我的下拉子菜单。 I want the user to be able to open it when hovered over the corresponding tag.我希望用户将鼠标悬停在相应的标签上时能够打开它。 I tried to make #lablinksDD open when hovered over #menuDD with #labLinksDD:hover #ddSbMenu How may I do that?我试图用#lablinksDD #labLinksDD:hover #ddSbMenu鼠标悬停在#menuDD上时打开#lablinksDD 我该怎么做? I feel like I am missing something.我觉得我错过了什么。

Code:代码:

HTML HTML

<ul>
                    <li><a href="DD/Index.html" class="tpNavBtns">Digital Design (DD) ▾</a></li> <!--▴-->

                    <ul class="subMenu1" id="menuDD">
                        <li><a href="#" id="labLinksDD">Labs ▸</a></li> <!--◂-->

                            <ul class="drpDn-subMenu labSbMenu" id="ddSbMenu">
                                <li><a href="DD/Labs/Lab_01/LB1_WeiJianZhen_DD.html">DD Lab 1</a></li>
                                <li><a href="DD/Labs/Lab_02/LB2_WeiJianZhen_DD.html">DD Lab 2</a></li>
                                <li><a href="DD/Labs/Lab_03/Lab_3.html">DD Lab 3</a></li>
                                <!--more than 20 a tags inside lists-->
                            </ul>

CSS CSS


.drpDn-subMenu {
    display: none;
    position: absolute;
    width: 200px;
    height: 500px;
    top: 0px;
    left: 164px;
    overflow-x: hidden;
    overflow-y: scroll;
    background-color: red;
    list-style-type: none;
    border-left: 1px solid rgba(120, 120, 120, 0.70);
    border-bottom: 1px solid rgba(120, 120, 120, 0.70);
}

.drpDn-subMenu a {
    display: inline-block;
    position: relative;
    padding: 20px 50px;
    width: 100%;
    height: auto;
    text-align: left;
}

#menuDD {
    top: 100%;
    left: 13.6%;
}

#menuWCP {
    top: 50px;
    left: 800px;
}

.labSbMenu {
    z-index: 3;
}

.prSbMenu {
    top: 56px;
    height: auto;
    background-color: antiquewhite;
}


I'll also accept a Vanilla JavaScript version instead of a CSS one.我还将接受香草 JavaScript 版本,而不是 CSS 版本。

Not sure that's what you're looking for, but you could use a Javascript function:不确定这就是您要查找的内容,但您可以使用 Javascript function:

<li><a href="#" id="labLinksDD" onclick="showLabLinks()">Labs ▸</a></li>


<script>
        function showLabLinks() {
        document.getElementById("ddSbMenu").style.display = "block";
        }
</script>

If you change the id from the anchor to the list item or add different id or class to the list item then you can solve this with CSS general sibling selector (~) or adjacent sibling selector (+) without JS, see https://www.w3schools.com/css/css_combinators.asp :如果您将 id 从锚点更改为列表项或添加不同的 id 或 class 到列表项,那么您可以使用 CSS 通用兄弟选择器 (~) 或不带 JS 的相邻兄弟选择器 (+) 解决此问题,请参阅Z5E056C500A1C48BADEA71 www.w3schools.com/css/css_combinators.asp

#labLinksDD:hover~#ddSbMenu {
    display: block;
}

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

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