隐藏/显示菜单 onclick javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35579569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:55:05  来源:igfitidea点击:

Hide/show menu onclick javascript

javascripthtmlmenuonclickshow-hide

提问by pythonious

I want to do a hide/show menu that shows when you click the p-tag with id menuand hides when you click the same p-tag — but apparently I'm useless on JavaScript. I don't want to use jQuery, I really want to learn this! Someone out there?

我想做一个隐藏/显示菜单,当您单击p带有 id的-tag时显示menu并在单击相同的p-tag时隐藏- 但显然我对 JavaScript 毫无用处。我不想使用jQuery,我真的很想学习这个!外面有人吗?

Here is my HTML:

这是我的 HTML:

<nav>
    <p id="menu"> Menu</p>
        <ul id="menu-box">
            <li><a href="index.html">Start</a></li>
            <li><a href="animal.html">Animal</a></li>
            <li><a href="pictures.html">Pictures</a></li>
        </ul>
    </nav>  

Here is my JavaScript (so far):

这是我的 JavaScript(到目前为止):

function setUp() {
    document.getElementById("menu").onclick = setMenu;
}

function setMenu() {
    var menuBox = document.getElementById("menu-box");
    menuBox.className = "menuBox";   
}

setUp();

回答by Thomas Orlita

function toggleMenu() {
  var menuBox = document.getElementById('menu-box');    
  if(menuBox.style.display == "block") { // if is menuBox displayed, hide it
    menuBox.style.display = "none";
  }
  else { // if is menuBox hidden, display it
    menuBox.style.display = "block";
  }
}
<p id="menu" onclick="toggleMenu()"> Menu</p>
<ul id="menu-box" style="display: block">
  <li><a href="index.html">Start</a></li>
  <li><a href="animal.html">Animal</a></li>
  <li><a href="pictures.html">Pictures</a></li>
</ul>

回答by Asm Hijas

If your learning this drop down ,then i suggest you to learn it from this button :)

如果你学习这个下拉菜单,那么我建议你从这个按钮学习它:)

<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

and add this style

并添加此样式

/* Dropdown Button */
.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
    position: relative;
    display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

/* Links inside the dropdown */
.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}

Now toggle the hide and show with javascript

现在使用 javascript 切换隐藏和显示

When the user clicks on the button, toggle between hiding and showing the dropdown content

当用户点击按钮时,在隐藏和显示下拉内容之间切换

function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}

回答by Szymon Pajka

Use this:

用这个:

window.nav = {
  status: false,
  menu: function(){
    if (nav.status){
      // here put class when menu is open - eg:
      document.getElementById("menu-box").className = "menu-box menuopen";
      nav.status = false;
    } else {
      // here put class when menu is closed - eg:
      document.getElementById("menu-box").className = "menu-box";
      nav.status = true;
    } 
  }
}
.menu-box{
  display: none;
}

.menuopen{
  display: block;
}
<nav>
                <p id="menu" onclick="nav.menu()"> Menu</p>
                <ul id="menu-box">
                    <li><a href="index.html">Start</a></li>
                    <li><a href="animal.html">Animal</a></li>
                    <li><a href="pictures.html">Pictures</a></li>
                </ul>
            </nav>