简体   繁体   English

使用纯JavaScript激活下拉菜单

[英]Activate dropdown with pure javascript

Right now I have a pure HTML CSS navigation bar with some dropdowns. 现在,我有一个带有一些下拉菜单的纯HTML CSS导航栏。 However on ipad the hover will obviously not work. 但是,在ipad上,悬停显然将无法工作。

I want to add a click event to the relevant menu items so the dropdown will also activate with an onclick event. 我想将click事件添加到相关菜单项中,因此下拉菜单也将通过onclick事件激活。

I've look at other answers but I'm not capable of reading javascript well enough so that I can modify them for my specific site. 我已经查看了其他答案,但是我无法很好地阅读javascript,因此无法为自己的特定网站进行修改。

here is a link to where I'm at now: http://2ftrade.nl/kareem/eindopdracht/ 这是我现在所在的位置的链接: http : //2ftrade.nl/kareem/eindopdracht/

and this is the relevant html. 这是相关的html。 In my css the default is display:none for the dropdown menus and is changed to display:block when hovered over the li that contains it. 在我的CSS中,下拉菜单的默认值为display:none,当悬停在包含它的li上时,其默认值为display:block。

        <ul>
            <li><a href=" " title="">Home</a></li>
            <li><a title="">Opleiding</a> 

                <!-- the dropdown -->
                <ul>
                    <li><a href="#" title="">Visie &amp; Beleid</a></li>
                    <li><a href="#" title="">Opbouw Studieprogramma</a></li>
                    <li><a href="#" title="">Competenties</a></li>
                    <li><a href="#" title="">Diploma</a></li>
                    <li><a href="#" title="">Beroepen</a></li>
                </ul>

            </li>
            <li><a href="#" title="">Onderwijsprogramma</a></li>
            <li><a href="#" title="">Organisatie</a></li>
            <li><a title="">Stages en Projecten</a>

                <!-- another dropdown -->
                <ul>
                    <li><a href="#" title="">Stages</a></li>
                    <li><a href="#" title="">Projecten</a></li>
                </ul>

            </li>
            <li><a href="#home">Top</a></li>
        </ul>

This is the css that hides the dropdown section 这是隐藏下拉部分的CSS

nav > ul > li > ul {
            display: none;
            position: absolute;
        }

and this is what will display it when hovering 这就是悬停时将显示的内容

 nav > ul > li:hover ul {
            display: block;
        }

you can attach event listener to your element: 您可以将事件侦听器附加到您的元素:

var dropdown_button = document.getElementById('#your-button-that-activates-dropdown');

dropdown_button.addEventListener('click', function() {
  //here do what you want to do when the button is clicked.
}, false);

you should use javascript events , some thing like this : 您应该使用javascript事件,例如:

var btn = document.getElementById('btn') // this button is a key to run what you want
var drp = document.getElementById('drp') // this is your dropdown list
btn.onclick = function()
{
    drp.style.display = 'block'
    // other codes . . .
}

You can achieve this without using javascript. 您无需使用javascript就可以实现这一目标。

Use the :target selector 使用:target选择器

example

Add an id and href for each target in the html 为html中的每个目标添加一个idhref

<ul>
    <li><a href=" " title="">Home</a></li>
    <li id="Opleiding">
    <a title="" href="#Opleiding">Opleiding</a> 

            <!-- the dropdown -->
            <ul>
                <li><a href="#" title="">Visie &amp; Beleid</a></li>
                <li><a href="#" title="">Opbouw Studieprogramma</a></li>
                <li><a href="#" title="">Competenties</a></li>
                <li><a href="#" title="">Diploma</a></li>
                <li><a href="#" title="">Beroepen</a></li>
            </ul>

        </li>
        <li><a href="#" title="">Onderwijsprogramma</a></li>
        <li><a href="#" title="">Organisatie</a></li>
        <li id="StagesenProjecten">
        <a href="#StagesenProjecten" title="">Stages en Projecten</a>

            <!-- another dropdown -->
            <ul>
                <li><a href="#" title="">Stages</a></li>
                <li><a href="#" title="">Projecten</a></li>
            </ul>

        </li>
        <li><a href="#home">Top</a></li>
    </ul>

in the css specify the style for the :target 在CSS中指定:target的样式

nav > ul > li:target ul {
            display: block;
        }

nav > ul > li:hover ul {
            display: block;
        }
nav > ul > li > ul {
            display: none;
            position: absolute;
        }

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

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